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:
Step 1: Understand row-by-row update need
When updating salaries individually, you must fetch each employee row and update inside the loop.
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.
Final Answer:
Declare cursor for SELECT, open it, fetch each employee, update salary inside loop, close and deallocate -> Option B
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
Master "Stored Procedures and Functions" in SQL
9 interactive learning modes - each teaches the same concept differently