70-465 · Question #97
You need to ensure that a stored procedure fails if an INSERT statment within the stored procedure fails. What action should you take?
The correct answer is C. SET XACT_ABORT ON. To ensure a stored procedure fails and rolls back its transaction if an INSERT statement within it fails, enable the XACT_ABORT option.
Question
Options
- ATHROW 51000, 'Abort!'
- BSET XACT_ABORT OFF
- CSET XACT_ABORT ON
- DTRY....CATCH
How the community answered
(67 responses)- A3% (2)
- B1% (1)
- C94% (63)
- D1% (1)
Why each option
To ensure a stored procedure fails and rolls back its transaction if an INSERT statement within it fails, enable the XACT_ABORT option.
THROW raises an exception programmatically but does not automatically ensure transaction rollback for all types of run-time errors that an INSERT statement might encounter unless explicitly handled in a CATCH block.
SET XACT_ABORT OFF means that if a T-SQL statement raises a run-time error, the transaction might not be rolled back entirely, and execution could continue, which is the opposite of the desired behavior.
SET XACT_ABORT ON ensures that if a T-SQL statement, such as an INSERT, within an explicit or implicit transaction encounters a run-time error, the entire transaction is automatically rolled back and the batch is terminated. This behavior guarantees that the stored procedure fails completely if the INSERT statement fails.
TRY...CATCH blocks are used for error handling and allowing recovery or custom error actions, but they do not inherently guarantee that an entire transaction will be rolled back upon an INSERT failure without explicit ROLLBACK TRANSACTION statements within the CATCH block.
Concept tested: SQL Server Transaction Abort Behavior (XACT_ABORT)
Source: https://learn.microsoft.com/en-us/sql/t-sql/statements/set-xact-abort-transact-sql
Topics
Community Discussion
No community discussion yet for this question.