0
0
SQLquery~5 mins

WHILE loops in procedures in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a WHILE loop in SQL procedures?
A WHILE loop repeatedly executes a block of SQL code as long as a specified condition is true.
Click to reveal answer
beginner
How do you start a WHILE loop in a SQL procedure?
You start with the keyword WHILE followed by a condition, then the code block inside BEGIN and END.
Click to reveal answer
beginner
Why is it important to change the condition inside a WHILE loop?
To avoid an infinite loop, you must update the condition inside the loop so it eventually becomes false.
Click to reveal answer
intermediate
Example: What does this WHILE loop do? DECLARE @i INT = 1; WHILE @i <= 3 BEGIN PRINT @i; SET @i = @i + 1; END
It prints the numbers 1, 2, and 3 one by one, increasing @i by 1 each time until @i is greater than 3.
Click to reveal answer
intermediate
Can WHILE loops be nested inside other WHILE loops in SQL procedures?
Yes, you can put a WHILE loop inside another WHILE loop to repeat complex tasks multiple times.
Click to reveal answer
What keyword starts a WHILE loop in a SQL procedure?
ALOOP
BREPEAT
CFOR
DWHILE
What happens if the condition in a WHILE loop never becomes false?
AThe loop stops automatically
BThe loop runs forever (infinite loop)
CThe procedure throws an error
DThe loop runs once
Where do you put the code that repeats inside a WHILE loop?
ABetween BEGIN and END
BAfter the WHILE keyword only
COutside the procedure
DInside a SELECT statement
Which of these is a valid condition for a WHILE loop?
ASET @count = 0
BPRINT @count
C@count < 10
DDECLARE @count INT
Can you use a WHILE loop to repeat a task a fixed number of times?
AYes, by controlling the loop condition
BNo, WHILE loops run only once
COnly if you use a FOR loop inside
DOnly in SELECT statements
Explain how a WHILE loop works inside a SQL procedure and why updating the loop condition is important.
Think about what happens if the condition never changes.
You got /3 concepts.
    Describe a simple example of a WHILE loop in a SQL procedure that counts from 1 to 5.
    Start with a variable set to 1 and increase it until 5.
    You got /4 concepts.