DECLARE @Min INT = 1, @Max INT = 10;
DECLARE @Result TABLE (ID INT);
WHILE @Min <= @Max
BEGIN
INSERT INTO @Result VALUES (@Min);
SET @Min = @Min + 1;
END
SELECT * FROM @Result;
Here’s the sequential breakdown of above SQL query execution flow:
- Variables
@Minand@Maxare initialized with values 1 and 10. - A table variable
@Resultis declared to hold integer values. - The
WHILEloop begins, checking if@Minis less than or equal to@Max. - Each loop iteration inserts the current
@Minvalue into@Result. @Minis incremented by 1 to move toward the termination condition.- Loop continues until
@Minexceeds@Max. - Final
SELECTretrieves and displays all inserted values from@Result.
