Online: 8088
Similar to IF condition, WHILE loop also works in the same ways as it works 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 iterates to the next iteration of the loop.