How can I set all values in OrderNumber column from 1 to n(when they are ordered by primary key) where n is entries count in table?
EDIT:
Let’s assume we have 3 entries with ID’s 4,7 and 15 … I want to set their OrderValues 1,2 and 3.
I’m using SQL Server 2008.
However, why on earth do you want to do this, when you can derive this information using the
ROW_NUMBER()function at query time? Storing the values means that they are guaranteed to be out of date and out of sync the moment you insert/update/delete a single row in the table. So unless you plan to run this update after every DML operation (e.g. using a trigger), which isn’t very logical to me, you’re likely much better off getting these row_number values when you run the query vs. storing them in the table.