UPDATE with ORDER BY

后端 未结 6 2323
Happy的楠姐
Happy的楠姐 2020-12-11 01:30

Need to "tie" UPDATE with ORDER BY. I\'m trying to use cursors, but get the error:

cursor "cursupd"         


        
6条回答
  •  忘掉有多难
    2020-12-11 02:19

    UPDATE with ORDER BY:

    UPDATE thetable 
      SET columntoupdate=yourvalue 
     FROM (SELECT rowid, 'thevalue' AS yourvalue 
             FROM thetable 
            ORDER BY rowid
          ) AS t1 
    WHERE thetable.rowid=t1.rowid;
    

    UPDATE order is still random (I guess), but the values supplied to UPDATE command are matched by thetable.rowid=t1.rowid condition. So what I am doing is, first selecting the 'updated' table in memory, it's named t1 in the code above, and then making my physical table to look same as t1. And the update order does not matter anymore.

    As for true ordered UPDATE, I don't think it could be useful to anyone.

提交回复
热议问题