Bird
0
0

You want to create a procedure that inserts numbers 1 to 5 into a table named Numbers using a WHILE loop. Which snippet correctly performs this?

hard📝 Application Q8 of 15
SQL - Stored Procedures and Functions
You want to create a procedure that inserts numbers 1 to 5 into a table named Numbers using a WHILE loop. Which snippet correctly performs this?
ADECLARE @i INT = 1; WHILE @i < 5 BEGIN INSERT INTO Numbers VALUES (@i); END
BDECLARE @i INT = 1; WHILE @i <= 5 BEGIN INSERT INTO Numbers VALUES (@i); SET @i = @i + 1; END
CDECLARE @i INT = 1; WHILE @i <= 5 BEGIN INSERT INTO Numbers VALUES (@i); SET @i = @i - 1; END
DDECLARE @i INT = 5; WHILE @i >= 1 BEGIN INSERT INTO Numbers VALUES (@i); SET @i = @i + 1; END
Step-by-Step Solution
Solution:
  1. Step 1: Check loop condition and increment

    DECLARE @i INT = 1; WHILE @i <= 5 BEGIN INSERT INTO Numbers VALUES (@i); SET @i = @i + 1; END loops from 1 to 5, increments @i correctly.
  2. Step 2: Verify insert statement and loop logic

    Inserts @i value each iteration; loop ends after 5.
  3. Final Answer:

    DECLARE @i INT = 1; WHILE @i <= 5 BEGIN INSERT INTO Numbers VALUES (@i); SET @i = @i + 1; END -> Option B
  4. Quick Check:

    Proper increment and condition ensure correct inserts [OK]
Quick Trick: Increment loop variable and check condition to insert correctly [OK]
Common Mistakes:
  • Missing increment causing infinite loop
  • Wrong loop condition excluding last number
  • Decrementing instead of incrementing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes