Bird
0
0

You want to update the salary of employees one by one using a cursor. Which approach correctly uses a cursor for this task?

hard📝 Application Q15 of 15
SQL - Stored Procedures and Functions
You want to update the salary of employees one by one using a cursor. Which approach correctly uses a cursor for this task?
AOpen cursor, but never fetch rows, then update salary
BDeclare cursor for SELECT, open it, fetch each employee, update salary inside loop, close and deallocate
CDeclare cursor, but update salary before opening cursor
DUse a single UPDATE statement without cursor for all employees
Step-by-Step Solution
Solution:
  1. Step 1: Understand row-by-row update need

    When updating salaries individually, you must fetch each employee row and update inside the loop.
  2. Step 2: Identify correct cursor usage

    Declare cursor for SELECT, open it, fetch each employee, update salary inside loop, close and deallocate correctly declares, opens, fetches, updates inside loop, then closes and deallocates cursor.
  3. Final Answer:

    Declare cursor for SELECT, open it, fetch each employee, update salary inside loop, close and deallocate -> Option B
  4. Quick Check:

    Cursor updates row-by-row inside loop [OK]
Quick Trick: Update inside fetch loop after opening cursor [OK]
Common Mistakes:
  • Trying to update before opening cursor
  • Not fetching rows before update
  • Using cursor but updating all at once

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes