Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a stored procedure loop

Status
Not open for further replies.

vamoose

Programmer
Joined
Oct 16, 2005
Messages
320
Location
MX
Can someone please tell or show me the basic principle on how to make a MSSQL 2000 stored procedure do a loop. Something like in the VBA example:

For Tester = 1 to 30
If something then something
Next Tester

Thanks for your assistance
 
HOwever, if at all possible it is best to avoid doing loops becasue loops are performance hogs. What are you planning on doing in the loop, it may be possible to do it in a set-based fashion instead.

"NOTHING is more important in a database than integrity." ESquared
 
Here the example:
Code:
DECLARE @Tester int
SET @Tester = 1
WHILE @Tester < 31
      BEGIN
        IF something
           BEGIN
           END
        ELSE
           BEGIN
           END

        SET @Tester = @Tester + 1
      END
but I am with SQL Sister here. Try to avoid loops if you could do what you want with set based commands.


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
Microsoft MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top