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 2000 Forums
 SQL Server Development (2000)
 Looping a Stored Procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-06 : 09:58:24
Darren Hitchcock writes "I am only using the SQL Server 2000 application and as part of studying for the MCP I need to run an sp within a loop. How can I do this?"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2001-12-06 : 11:04:41
The WHILE statement is the SQL Server loop control. Check Books Online for examples and details, but it works something like this:

WHILE <logical test>
BEGIN
...put code to be looped here
END


An example:

WHILE @counter<10
BEGIN
UPDATE myTable SET Weight=LOG(Weight) WHERE IQ=@counter
--instant weight loss program for idiots!
SELECT @counter=@counter + 1
END


Just a friendly warning: MAKE ABSOLUTELY SURE you don't create an infinite loop. Your stored procedure won't be able to terminate a loop that's external to it, so be sure to include some code that will stop the loop at some point.

Go to Top of Page
   

- Advertisement -