Bird
0
0

Examine the following cursor code snippet:

medium📝 Debug Q6 of 15
SQL - Stored Procedures and Functions
Examine the following cursor code snippet:
DECLARE cur CURSOR FOR SELECT id FROM products;
FETCH NEXT FROM cur INTO @prod_id;
OPEN cur;
CLOSE cur;
DEALLOCATE cur;

What is the error in this code?
ACursor is not declared properly
BFETCH NEXT is called before OPENing the cursor
CDEALLOCATE should come before CLOSE
DMissing FETCH PRIOR statement
Step-by-Step Solution
Solution:
  1. Step 1: Review cursor lifecycle

    A cursor must be OPENed before FETCH operations.
  2. Step 2: Identify the sequence error

    FETCH NEXT is called before OPEN cur, which is invalid.
  3. Final Answer:

    FETCH NEXT is called before OPENing the cursor -> Option B
  4. Quick Check:

    Always OPEN cursor before FETCH [OK]
Quick Trick: OPEN cursor before FETCH [OK]
Common Mistakes:
  • Calling FETCH before OPEN
  • Confusing order of CLOSE and DEALLOCATE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes