Updated on Kisan Patel
This tutorial will show you how to use WHILE
loop in SQL Server?
Similar to IF
condition, WHILE
loop is also used as in other programming language.
DECLARE @count int = 10 WHILE (@count < 15) BEGIN IF (@count = 13) BEGIN BREAK; END ELSE BEGIN SELECT @count AS thisCounter SET @count = @count + 1 CONTINUE; END END
In the above code snippet, the WHILE
loop is marked to run 5 times however when the value of @count
is reaching 13, we are breaking the loop using BREAK
statement otherwise CONTINUE
statement iterate to the next iteration of the loop.