I have to update multiple rows(29) in a table in SQL server 2005.
SELECT * from tblState ORDER BY StateCode ASC.
In this table there’s a integer column which I need to assign numbers starting from 1 to 29. For e.g.
BEFORE
A 3
B 6
C 2
D 1
AFTER
A 1
B 2
C 3
D 4
How can I update each row with a ascending sequential number efficiently?
As you are asking for a loop I guess you may not understand this code.
As such, I strongly recommend you create a dummy table to play with this, both to understand that how it works, and to ensure it does what you want/expect.
WITHstatement is similar to a sub queryROW_NUMBER()creates the sequential ids that you want