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
 General SQL Server Forums
 New to SQL Server Programming
 loop is sql

Author  Topic 

craigmacca
Posting Yak Master

142 Posts

Posted - 2008-09-01 : 11:34:09
Hi i need a stored proc that does the following

1. select Distinct(ParentId) from table A
2. then for each ParentId loop through and exec another stored proc (which i already have) passing the ParentId as the addin param

just not sure what the best way to do this is?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-01 : 11:36:00
[code]DECLARE @ID VARCHAR(100)

SELECT @ID = MIN(ParentID)
FROM TableA

WHILE @ID IS NOT NULL
BEGIN
EXEC AnotherSP @ID

SELECT @ID = MIN(ParentID)
FROM TableA
WHERE ParentID > @ID
END[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -