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 |
craigmacca
Posting Yak Master
142 Posts |
Posted - 2008-12-26 : 06:46:06
|
Hi i have the query below in a stored proc, i need to loop through this an update the Ordinal column with a count ie 1,2,3 etcnot sure how to loop through i have tried a few differnt things?SELECT t.Id, t.OrdinalFROM dbo.Tree tINNER JOIN dbo.Page p ON p.Id = t.PageIdWHERE p.ParentId = @ParentIdOrder By t.Ordinal |
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-12-26 : 07:06:17
|
Hi,Please post your table structures,sample data and expected output so that u will get ur question answered quickly... |
|
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2008-12-26 : 07:25:08
|
all i need to know is how to loop through the query, the update statement will be this@count int = 0START WHILE LOOP@count = @count +1Update TreeSET Ordinal = @count WHERE Id = --THE ID FROM THE SELECT QUERYEND WHILE LOOP |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-26 : 23:12:18
|
declare @cnt intset @cnt = 1while(@cnt > 3)--(give the value how many times u want the loop to execute)beginUpdate TreeSET Ordinal = @count WHERE Id = --THE ID FROM THE SELECT QUERYselect @cnt = @cnt + 1endtry like this |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-12-27 : 00:53:34
|
declare @count intset @count=0Update TreeSET Ordinal=@count, @count=@count+1WHERE Id = --THEMadhivananFailing to plan is Planning to fail |
|
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2008-12-27 : 06:40:26
|
Hi i know how to loop i need to know how to loop through a queryso this is what i need1. SELECT QUERYSELECT t.Id, t.OrdinalFROM dbo.Tree tINNER JOIN dbo.Page p ON p.Id = t.PageIdWHERE p.ParentId = @ParentIdOrder By t.Ordinal2. LOOP THROUGH THE QUERY ABOVE@count int = 0START LOOP@count = @count +1Update TreeSET Ordinal = @count WHERE Id = --THE ID FROM THE SELECT QUERYEND LOOPSo i need to know how to loop through the query and pass the Id to the update statement |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-27 : 11:58:48
|
seems like what you need is thisUPDATE rSET r.Ordinal=r.SeqFROM(SELECT t.Id, t.Ordinal,ROW_NUMBER() OVER(ORDER BY t.Ordinal) AS SeqFROM dbo.Tree tINNER JOIN dbo.Page p ON p.Id = t.PageIdWHERE p.ParentId = @ParentId)r |
|
|
|
|
|