简体   繁体   中英

FETCH FIRST n ROWS ONLY within subquery using Java Persistence Query Language

I'm trying to do a bulk update of DB2 table using Java Persistence Query Language:

UPDATE Account  a
SET a.Status = 'r', a.Code = :code, a.Timestamp = CURRENT_TIMESTAMP
WHERE ClientNumber IN  (SELECT ClientNumber FROM Account
                        WHERE a.Status = '' AND a.Type = :type 
                        ORDER BY a.Code ASC
                        FETCH FIRST 5 ROWS ONLY)

However, FETCH FIRST n ROWS ONLY appears not to be supported in JPQL.

What are the other alternatives exist to do this?

There's no way to execute such a query in JPQL. You'll have to use SQL.

Or you could execute the following query:

SELECT a FROM Account a WHERE a.Status = '' AND a.Type = :type

and call setMaxResults(5) before executing the query. You would get 5 accounts (maximum) as a result and you could change the status, the code and the timestamp of the 5 returned accounts.

I find it strange to get the 5 first rows of a query which doesn't have any order by clause, though.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM