SQL Server Transaction Nesting

Source: Internet
Author: User
Tags savepoint

Example code:

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 are an    --error.    SAVE TRANSACTIONProceduresave;ELSE    --Procedure must start its own    --transaction.    BEGIN TRANSACTION;--Modify database.BEGINTRY/** 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;ENDTRYBEGINCATCH--An error occurred; must determine    --which type of rollback would roll    -- back is only the    --procedure.    IF @TranCounter = 0        --Transaction started in procedure.        --Roll back to complete transaction.        ROLLBACK TRANSACTION; ELSE        --Transaction started before procedure        --called, don't roll back modifications        --made before the procedure was called.        IFXact_state ()<> -1            --If The transaction is still valid, just            --SavePoint set at the            --start of the stored procedure.            ROLLBACK TRANSACTIONProceduresave; --If The transaction is uncommitable, a            --rollback to the savepoint are 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);ENDCatch

SQL Server Transaction Nesting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.