In this article, we will learn how to use the WHILE loop in SQL.
The WHILE loop loops through a block of code as long as a specified condition is not false.
The WHILE loop is handy because they save time, reduces errors, and they make code more readable.
Syntax
WHILE <condition> BEGIN -- Statement block END;
Example
The subsequent statement would print 1 to 10 digits:
DECLARE @i INT = 1; WHILE @i <= 10 BEGIN PRINT @i; SET @i = @i + 1; END;
Also, check How To Use Case Statement In SQL