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條評論
主站蜘蛛池模板: 久久福利资源站免费观看i 久久高清 | 久免费视频| 亚洲国产伦理 | 国产精品久久久久久久 | 国产欧美一级片 | 91在线网址| 久久国产经典视频 | 伊人2233| 国产在线欧美日韩一区二区 | 天天干天天操天天做 | 亚洲一区二区三区国产精品 | 曰本一区二区 | 女人十八毛片免费观 | 久久精品高清 | s8国产成人精品视频 | 日日夜夜免费精品视频 | 老司机午夜免费 | 华人欧美国产在线精品 | 国产91精品一区二区视色 | 夜精品a一区二区三区 | 四虎影视在线影院4hu | 成人夜色香网站在线观看 | 日本免费特黄aa毛片 | 大片在线播放日本一级毛片 | 欧美ⅹxxxx18性欧美 | 九九热在线精品视频 | 久久这里只有精品国产99 | 国产精品怡红院永久免费 | 五十路一区二区三区视频 | 色妞www精品视频 | 日韩精品一区二区三区中文在线 | 精品久久久久久久一区二区手机版 | 69福利视频 | 国产69精品久久久久99不卡 | 亚洲精品a| 国产精品成人久久久久 | 成人夜色视频网站在线观看 | 精品老司机在线观看视频 | 乱子伦xxxx厨房 | 91精品在线免费 | 老司机精品在线播放 |