70-464 Exam QuestionsBrowse all questions from this exam

70-464 Exam - Question 53


You use SQL Server to maintain the data used by applications at your company.

You need to run two separate SQL statements.

You must guarantee that the following three things happen:

Either BOTH statements succeed or BOTH statements fail as a batch.

If an error occurs on the first statement, SQL should not attempt to run the second statement.

Error information should be returned to the client.

What should you do?

Show Answer
Correct Answer: B

To ensure both statements succeed or fail as a batch and error information is returned to the client, using SET XACT_ABORT ON and a TRY...CATCH block is the most effective approach. The TRY...CATCH block captures any errors that occur during execution, allowing the transaction to be rolled back safely if an error occurs, thereby fulfilling all three requirements: both statements succeed or fail together, the second statement is not executed if the first fails, and error information is captured and returned to the client. Therefore, the best option is to use SET XACT_ABORT ON combined with the TRY...CATCH pattern.

Discussion

1 comment
Sign in to comment
LiamLin
Aug 31, 2021

B not return Error Information to client A is correct SET XACT_ABORT ON BEGIN TRANSACTION PRINT 1 PRINT 1/0 PRINT 2 IF @@ERROR <> 0 BEGIN PRINT 'ROLLBACK' ROLLBACK END ELSE BEGIN PRINT 'COMMIT' COMMIT TRANSACTION END GO