Bird
0
0

How can you combine a cursor with transaction control to ensure all row updates succeed or none do?

hard📝 Application Q9 of 15
SQL - Stored Procedures and Functions
How can you combine a cursor with transaction control to ensure all row updates succeed or none do?
ABegin a transaction before opening cursor, commit after loop, rollback on error
BOpen cursor inside a transaction but commit after each row update
CUse cursor without transaction; rely on auto-commit
DDeclare cursor inside a transaction but never commit or rollback
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction scope with cursor

    Transaction should start before cursor operations to cover all updates.
  2. Step 2: Commit or rollback after processing all rows

    Commit after successful loop; rollback if any error occurs to keep data consistent.
  3. Final Answer:

    Begin a transaction before opening cursor, commit after loop, rollback on error -> Option A
  4. Quick Check:

    Transaction wraps entire cursor operation [OK]
Quick Trick: Wrap cursor loop in transaction for all-or-nothing updates [OK]
Common Mistakes:
  • Committing after each row
  • No rollback on error
  • Ignoring transaction control

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes