snippetsqlMinor
How to UPDATE just one record in DB2?
Viewed 0 times
updatejustdb2onerecordhow
Problem
In DB2, I need to do a
But I need to make sure to update only one record per transaction.
Familiar with the
places a limit on the number of rows that can be updated
I looked for something similar in DB2's
How can something similar be achieved in DB2?
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?
Solution
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
So first, you would have to declare a cursor from your table with the select statement that would give you what you want.
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
I believe you need to issue a
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.
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.
Code Snippets
DECLARE C1 CURSOR
FOR (SELECT * FROM MYTABLE WHERE...);UPDATE MYTABLE
SET MYCOLUMN = ?
WHERE CURRENT OF C1;Context
StackExchange Database Administrators Q#10176, answer score: 3
Revisions (0)
No revisions yet.