Which code should you use?

You use SQL Server 2014 to maintain the data used by applications at your company.
You want to execute two statements.
You need to guarantee that either both statements succeed, or both statements fail together as a batch.
Which code should you use?

You use SQL Server 2014 to maintain the data used by applications at your company.
You want to execute two statements.
You need to guarantee that either both statements succeed, or both statements fail together as a batch.
Which code should you use?

A.
Option A

B.
Option B

C.
Option C

D.
Option D
Option E

D.
Option D
Option E

Explanation:

Structure should be:
BEGIN TRY
BEGIN TRANSACTION
..
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH.



Leave a Reply 7

Your email address will not be published. Required fields are marked *


Yo

Yo

Only D is correct. If no transaction, one of them will still succeed.

Modi

Modi

I agree it is D only

wk

wk

D is correct

mk

mk

What about “C”? Is “C” really incorrect?!

Dim

Dim

C isn’t correct because @@error holds value for last statement.
if the first insert will fail while the second succeeded the @@error
value will be 0 and the transaction committed.