Skip to content

WHILE

SET @number = 0;
WHILE @number < 5 DO
SET @number = @number + 1;
SELECT @number;
END WHILE;
1
2
3
4
5

In the example provided, a variable @number is initialized as 0. The WHILE loop continues executing the statements within as long as @number is less than 5. Inside the loop, @number is incremented by 1 with each iteration. Consequently, the SQL script outputs the numbers from 1 to 5, inclusively.