Friday, August 31, 2012

How to use While Loop In StoredProcedures

Here I will explain how to use while loop in Stored Procedures. Mostly we will use while loop in stored procedures.
Here is the sample stored procedure to print 1 to 5 using while loop.

Ex : 1

create proc whileLoop
as
begin

declare @I int = 1, @J int = 5 -- Declare i, J and assign values

while(@I <= @J)
begin

print @I

set @I = @I + 1;

end

end

and now to execute the above stored procedure write the below line and select if the line is in the sample script file and press F5


exec whileLoop


Out Put :

1
2
3
4
5

No comments:

Post a Comment