SQL Server TRY ... CATCH with XACT_STATE

I have a question regarding MSDN documentation for blocks TRY CATCH. Browse through this article and scroll down to Example C, “Using TRY ... CATCH with XACT_STATE”

http://msdn.microsoft.com/en-us/library/ms175976.aspx

The example first places COMMIT TRANSACTIONin the Try block, and then puts the second in the Catch block, if XACT_STATE()=1.

However, I thought that the catch block would only be executed in case of an error. So, how could both Catch blocks execute and XACT_STATEreturn 1? This seems controversial.

The documentation XACT_STATEhas an unanswered comment that asks the same question

http://msdn.microsoft.com/en-us/library/ms189797.aspx

+5
source share
1

@user1181412 : :

- FOREIGN KEY.

-

- . , DELETE , COMMIT . XACT_STATE 1 CATCH.

SET XACT_ABORT ON;

, , , , :

-- Test whether the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
    PRINT
        N'The transaction is in an uncommittable state.' +
        'Rolling back transaction.'
    ROLLBACK TRANSACTION;
END;

, "SET XACT_ABORT OFF"; CATCH , "" XACT_STATE = 1.

. , , , :

( 1 ). . .

+2

All Articles