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.
| Author |
Topic |
|
hoyaabanks
Starting Member
1 Post |
Posted - 2006-05-06 : 17:43:33
|
| I have a data table that sort of looks like thisID Name Priority-------------------------31 Cars 2643 Motorcycles 2655 Boats 2756 Sail Boats 282 Ducks 3444 Skateboards 44The items are order on a web page based on their priority. I want to write a Stored Procedure to clean up the priority values every now and agian, as records are added and removed etc;....Once you run this procedure the same table should look like thisID Name Priority-------------------------31 Cars 143 Motorcycles 255 Boats 356 Sail Boats 42 Ducks 544 Skateboards 6So here is my basic Algorythem:a. Select Records from Transportation Order By Prioirtyb. Set the priority value to the order of the select statement starting with 1I am not sure how to accomplish the second part. How would I write that. Would I use a loop? How? How do you get the record number value? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-05-06 : 21:51:48
|
| update tblset Priority = (select count(*) from tbl t2 where t2.Priority < t1.Priority or (t2.Priority = t1.Priority and t2.ID , t1.ID)) + 1from tbl t1==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-06 : 23:41:53
|
minor typo error in nr's codeupdate tblset Priority = (select count(*) from tbl t2 where t2.Priority < t1.Priority or (t2.Priority = t1.Priority and t2.ID < t1.ID)) + 1from tbl t1 KH |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-05-07 : 17:31:46
|
| Given that this is for display on a web page I doubt if performance is an issue and the overhead of instantiating the cursor might be significant.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2006-05-07 : 17:42:42
|
| If the performance is an issue, then (as usual) it is good idea to test different implementations and find which one fits the best. |
 |
|
|
|
|
|