Submit the following sql query:
UPDATE MYTABLE
SET COL2 = (SELECT COL2 + 1 FROM (SELECT MAX(COL2) FROM MYTABLE) AS X)
WHERE ID IN (1,2,3,4,5)
Assume that before the update is complete, MAX (COL2) is 1.
My intention is that for an update where ID = 1 COL2 is updated to 'max (COL2) + 1' (i.e. 2), and for subsequent updates, MAX (COL2) + 1 'is overridden, so for ID = 2, COL2 = 3 and ID = 3, COL2 = 4, etc ...
What actually happens is that for all rows (ID = 1,2,3,4,5) the value of COL2 is 2.
Is there a smart way for MAX (COL2) +1 to be “revised” with every update? I understand that this may cause performance problems, but I'm curious no less! Is there a better alternative (which does not include multiple update statements)?
BTW: you might think about the syntax used for the above query (nested inner table), see here: SQL: using the target table in the UPDATE statement in the nested FROM clause
source
share