Mysql – How to UPDATE just one record in DB2

db2MySQLtransaction

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?

Best Answer

Actually, on the link you have, it mentions that you can update only one record, you just have to use cursors to do so. It then points you to the DECLARE CURSOR page.

So first, you would have to declare a cursor from your table with the select statement that would give you what you want.

DECLARE C1 CURSOR
FOR (SELECT * FROM MYTABLE WHERE...);

Move to the value of the record in the cursor that you want.

Then run you update statement against the cursor using the link you mention

UPDATE MYTABLE 
SET MYCOLUMN = ?
WHERE CURRENT OF C1;

I believe you need to issue a COMMIT (which closes the cursor unless you specify otherwise in the DECLARE CURSOR clause).

Check out the documentation in the link you have and in the one I have. You may end up having to use a stored procedure to run this.