Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Reorder of sortOrder column

Author  Topic 

yetti
Starting Member

1 Post

Posted - 2007-09-26 : 20:45:03
Hi all, I've been a lurker on SQL Team for a LOOOONG time and finally have a situation where I'd simply like some advice. I have a Resources table that stores picture data for one of our applications. One of the fields is sortOrder. If someone deletes the fifth picture, I would like to run an update on the delete trigger that will go through and reset the sort orders for the remaining photos correctly.

id_picture id_asset assetType sortOrder
-----------------------------------------------------
1234 17 'Rolex' 1
1235 17 'Rolex' 2
1236 17 'Rolex' 3
1237 17 'Rolex' 4
1238 17 'Rolex' 5
1239 17 'Rolex' 6
1240 17 'Rolex' 7


If you remove picture 1237, what is the best methodology for efficiently updating the remaining so that you get



id_picture id_asset assetType sortOrder
-----------------------------------------------------
1234 17 'Rolex' 1
1235 17 'Rolex' 2
1236 17 'Rolex' 3
1238 17 'Rolex' 4
1239 17 'Rolex' 5
1240 17 'Rolex' 6

I've looked into both cursors and while loops, was hoping someone with far more expertise can give me some insight. Code or a tutorial to point me in the correct direction would be very very helpful! TIA!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-26 : 20:56:07
do you need to store the ordering in the table ?

You can use row_number() over (order by <your ordering column list here>) for the sortOrder


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-09-26 : 21:01:21
If it is a sort order column, having a missing value will have no effect.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-09-26 : 21:02:10
quote:
Originally posted by jsmith8858

If it is a sort order column, having a missing value will have no effect.

- Jeff
http://weblogs.sqlteam.com/JeffS




Good point


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -