I want to update a table using jpql. Table structure and condition and query is below.
-------------------------------------------------------------
ASSIGNMENT | BUSINESS_AREA | DC_CURRENCY | DC_AMOUNT | STATUS
-------------------------------------------------------------
In above table structure I want to update all the rows status as completed where sum of DC_AMOUNT is ZERO and DC_CURRENCY is same for all rows , and ASSIGNMENT is also same
My approach
Query to get all assignment number satisfying condition
SELECT
q.assignment
FROM
OnAccoutEntity q, OnAccoutEntity r
WHERE
q.assignment = r.assignmet
AND q.dcCurrency = r.dcCurrency
AND q.businessArea = r.businessArea
GROUP BY
q.assignmnt
HAVING
SUM(q.dcAmount) = 0
UPDATE OnAccoutEntity p
SET p.status = 'COMPETED'
WHERE p.assignment IN ("Result of previous query' )
If I use single query to update it is giving exception.
java.sql.SQLException: You cant specify target 'ONACCCOUNT' for update in form clause.
Please suggest a better approach as this is time consuming.
It should do with an EXISTS ,
UPDATE
OnAccoutEntity o
SET
o.status = 'COMPLETED'
WHERE
EXISTS (SELECT
q.assignment
FROM
OnAccoutEntity q
WHERE
o.assignment = q.assignment
AND
q.dcCurrency = o.dcCurrency
AND
q.businessArea = r.businessArea
GROUP BY
q.assignment
HAVING SUM(q.dcAmount) = 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.