Skip to content
Advertisement

Reset or Update Row Position Integer in Database Table

I am working on a stored procedure in SQL Server 2008 for resetting an integer column in a database table. This integer column stores or persists the display order of the item rows. Users are able to drag and drop items in a particular sort order and we persist that order in the database table using this “Order Rank Integer”.

Display queries for items always append a “ORDER BY OrderRankInt” when retrieving data so the user sees the items in the order they previously specified.

The problem is that this integer column collects a lot of duplicate values after the table items are re-ordered a bit. Hence…

After a lot of reordering by the user becomes….

These duplicates are primarily because of insertions and user specified order numbers. We’re not trying to prevent duplicate order ranks, but we’d like a way to ‘Fix’ the table on item inserts/modifies.

Is there a way I can reset the OrderRankInt column with a single UPDATE Query? Or do I need to use a cursor? What would the syntax for that cursor look like?

Thanks, Kervin

EDIT

Update with Remus Rusanu solution. Thanks!!

Advertisement

Answer

This doesn’t account for any foreign key relationships, I hope you are taken care of those.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement