SQL Server 事務嵌套

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   for   sp   div   

範例程式碼:

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 TRYBEGIN 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 事務嵌套

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.