在BEGIN TRY块中有一个BEGIN TRANSACTION语句是错误的方法吗?

时间:2018-02-27 00:37:43

标签: sql-server-2008 transactions rollback

我想在我的SP中发现错误后回滚一个事务,所以我找到https://app-name-pxwlglhegg.now.sh/auth/github/callbackTRANSACTION块应该进入TRY块。

然而,当我运行那个例子时,我得到了:

Msg 6401, Level 16, State 1, Line 16
Cannot roll back MYTRAN. No transaction or savepoint of that name was found.

我想知道这种方法是正确的还是向后的,我的意思是TRY块应该进入TRANSACTION块。

修改

我正在运行上面链接中使用的非常类似的查询。

但是以防代码是

BEGIN TRY
        BEGIN TRANSACTION MYTRAN; -- Give the transaction a name

        SELECT 1/0  -- Generates divide by zero error causing control to jump into catch

        PRINT '>> COMMITING'
        COMMIT TRANSACTION MYTRAN;
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0
        BEGIN 
            PRINT '>> ROLLING BACK'
            ROLLBACK TRANSACTION MYTRAN; -- The semi-colon is required (at least in SQL 2012)

            SELECT
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_SEVERITY() AS ErrorSeverity,
        ERROR_STATE() AS ErrorState,
        ERROR_PROCEDURE() AS ErrorProcedure,
        ERROR_LINE() AS ErrorLine,
        ERROR_MESSAGE() AS ErrorMessage
        END
    END CATCH

0 个答案:

没有答案