In DB2, I need to do a SELECT FROM UPDATE, to put an update + select in a single transaction.
But I need to make sure to update only one record per transaction.
Familiar with the LIMIT clause from MySQL’s UPDATE option
places a limit on the number of rows that can be updated
I looked for something similar in DB2’s UPDATE reference but without success.
How can something similar be achieved in DB2?
Edit: In my scenario, I have to deliver 1000 coupon codes upon request. I just need to select (any)one that has not been given yet.
The question uses some ambiguous terminology that makes it unclear what needs to be accomplished. Fortunately, DB2 offers robust support for a variety of SQL patterns.
To limit the number of rows that are modified by an
UPDATE:The
UPDATEstatement never sees the base table, just the expression that filters it, so you can control which rows are updated.To
INSERTa limited number of new rows:This is how DB2 supports
SELECTfrom anUPDATE,INSERT, orDELETEstatement:The
SELECTwill return data from only the modified rows.