Skip to main content
2 of 2
deleted 71 characters in body; edited tags; edited title
Paul White
  • 90.3k
  • 30
  • 424
  • 663

Adding a sequence number to existing rows

I have a table with an OrderSequence column. The schema looks roughly like this:

id 
Name 
OrderSequence

I have an array of ids ["1", "2", "3", "4"] from an application, and I want to set the OrderSequence for sorting purposes. I don't want to loop through the rows from the application, executing 'n' SQL queries one at a time. How can I do this using a stored procedure?

Sample data

id Name OrderSequence
1  A    0
2  B    0
3  C    0

Desired result

id Name OrderSequence
1  A    1
2  B    2
3  C    3
Lee Gary
  • 153
  • 1
  • 1
  • 5