Complete the code to start a WHILE loop that runs as long as the counter is less than 5.
DECLARE @counter INT = 0; WHILE [1] BEGIN SET @counter = @counter + 1; END;
The WHILE loop continues as long as the condition is true. Here, it runs while @counter < 5.
Complete the code to increment the variable inside the WHILE loop.
DECLARE @i INT = 1; WHILE @i <= 3 BEGIN SET @i = [1]; END;
Inside the loop, we increase @i by 1 each time to eventually end the loop.
Fix the error in the WHILE loop condition to avoid an infinite loop.
DECLARE @num INT = 10; WHILE [1] BEGIN SET @num = @num - 1; END;
The loop should run while @num is greater than 0 to avoid running forever.
Fill both blanks to create a WHILE loop that counts down from 5 to 1.
DECLARE @count INT = 5; WHILE [1] BEGIN PRINT @count; SET @count = [2]; END;
The loop runs while @count is greater than 0, and decreases @count by 1 each time.
Fill all three blanks to create a WHILE loop that sums numbers from 1 to 5.
DECLARE @sum INT = 0; DECLARE @num INT = 1; WHILE [1] BEGIN SET @sum = @sum + [2]; SET @num = [3]; END;
The loop runs while @num is less than or equal to 5, adds @num to @sum, and increments @num by 1 each time.