0--Procedurecalledwhenthereis--anactivetransaction.--Createasavepointtobeable--torollbackonlytheworkdone--intheprocedureifthereisan--error.SAVE" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

SQL Server 事務嵌套

系統 2023 0
原文: SQL Server 事務嵌套

示例代碼:

      
        DECLARE
      
      
        @TranCounter
      
      
        INT
      
      
        ;




      
      
        SET
      
      
        @TranCounter
      
      
        =
      
      
        @@TRANCOUNT
      
      
        ;


      
      
        IF
      
      
        @TranCounter
      
      
        >
      
      
        0
      
      
        --
      
      
         Procedure called when there is
      
      
        --
      
      
         an active transaction.
      
      
        --
      
      
         Create a savepoint to be able
      
      
        --
      
      
         to roll back only the work done
      
      
        --
      
      
         in the procedure if there is an
      
      
        --
      
      
         error.
      
      
        SAVE
      
      
        TRANSACTION
      
      
         ProcedureSave;


      
      
        ELSE
      
      
        --
      
      
         Procedure must start its own
      
      
        --
      
      
         transaction.
      
      
        BEGIN
      
      
        TRANSACTION
      
      
        ;


      
      
        --
      
      
         Modify database.
      
      
        BEGIN
      
      
         TRY



    
      
      
        /*
      
      
        *

        Write your T-SQL here...

    *
      
      
        */
      
      
        --
      
      
         Get here if no errors; must commit
      
      
        --
      
      
         any transaction started in the
      
      
        --
      
      
         procedure, but not commit a transaction
      
      
        --
      
      
         started before the transaction was called.
      
      
        IF
      
      
        @TranCounter
      
      
        =
      
      
        0
      
      
        --
      
      
         @TranCounter = 0 means no transaction was
      
      
        --
      
      
         started before the procedure was called.
      
      
        --
      
      
         The procedure must commit the transaction
      
      
        --
      
      
         it started.
      
      
        COMMIT
      
      
        TRANSACTION
      
      
        ;


      
      
        END
      
      
         TRY


      
      
        BEGIN
      
      
         CATCH

    
      
      
        --
      
      
         An error occurred; must determine
      
      
        --
      
      
         which type of rollback will roll
      
      
        --
      
      
         back only the work done in the
      
      
        --
      
      
         procedure.
      
      
        IF
      
      
        @TranCounter
      
      
        =
      
      
        0
      
      
        --
      
      
         Transaction started in procedure.
      
      
        --
      
      
         Roll back complete transaction.
      
      
        ROLLBACK
      
      
        TRANSACTION
      
      
        ;

    
      
      
        ELSE
      
      
        --
      
      
         Transaction started before procedure
      
      
        --
      
      
         called, do not roll back modifications
      
      
        --
      
      
         made before the procedure was called.
      
      
        IF
      
       XACT_STATE() 
      
        <>
      
      
        -
      
      
        1
      
      
        --
      
      
         If the transaction is still valid, just
      
      
        --
      
      
         roll back to the savepoint set at the
      
      
        --
      
      
         start of the stored procedure.
      
      
        ROLLBACK
      
      
        TRANSACTION
      
      
         ProcedureSave;

            
      
      
        --
      
      
         If the transaction is uncommitable, a
      
      
        --
      
      
         rollback to the savepoint is not allowed
      
      
        --
      
      
         because the savepoint rollback writes to
      
      
        --
      
      
         the log. Just return to the caller, which
      
      
        --
      
      
         should roll back the outer transaction.
      
      
        --
      
      
         After the appropriate rollback, echo error
      
      
        --
      
      
         information to the caller.
      
      
        DECLARE
      
      
        @ErrorMessage
      
      
        NVARCHAR
      
      (
      
        4000
      
      
        );

    
      
      
        DECLARE
      
      
        @ErrorSeverity
      
      
        INT
      
      
        ;

    
      
      
        DECLARE
      
      
        @ErrorState
      
      
        INT
      
      
        ;



    
      
      
        SELECT
      
      
        @ErrorMessage
      
      
        =
      
      
         ERROR_MESSAGE();

    
      
      
        SELECT
      
      
        @ErrorSeverity
      
      
        =
      
      
         ERROR_SEVERITY();

    
      
      
        SELECT
      
      
        @ErrorState
      
      
        =
      
      
         ERROR_STATE();



    
      
      
        RAISERROR
      
       (
      
        @ErrorMessage
      
      , 
      
        @ErrorSeverity
      
      , 
      
        @ErrorState
      
      
        );




      
      
        END
      
       CATCH
    

?

SQL Server 事務嵌套


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品乱码一区二区三区在线 | 欧美亚洲综合图区在线 | 久久久精品久久久久特色影视 | 99热这里有免费国内精品 | 免费看国产精品久久久久 | 亚洲午夜久久久精品影院视色 | 日本中文字幕一区二区高清在线 | 欧美精品亚洲精品日韩一区 | 91久久线看在观草草青青 | 九九精品视频在线免费观看 | 丝袜三级| 不卡国产在线 | 日韩免费一级毛片 | 这里只有精品在线观看 | 97热在线| 久久草国产 | 国内精品久久久久久久 | 久草视频免费播放 | 日韩在线视频网址 | 免费看人做人爱视频拍拍拍 | 欧美日韩高清 | 亚洲国产精久久久久久久春色 | 2021国产精品久久久久 | 国产色综合网 | 精品视频一区在线观看 | 国产美女色视频 | 2020国产精品视频免费 | 国产麻豆精品hdvideoss | 国产精品久久久久孕妇 | 99国产精品免费观看视频 | 中文字幕亚洲精品日韩精品 | 亚洲 欧洲 另类 综合 自拍 | 久久色精品 | 国产成人啪午夜精品网站男同 | 性欧美一级毛片在线播放 | 尤物福利| 国产原创中文字幕 | 欧美男人天堂网 | 午夜一级在线 | 国产成人亚洲精品大帝 | 午夜大片免费男女爽爽影院久久 |