Stored Procedure Transaction

后端 未结 2 768
梦谈多话
梦谈多话 2021-02-04 04:29

I have never used a Transaction, Commit and Rollback before and now I need to use one. I have checked around online, etc for examples to make sure that I am in fact using this

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 05:20

    Oh well i rewrite quickly your SP using the concept TRY CATCH and the TRANSACTION as you requested but i didnt check it.

    This code will work in SQL 2005/2008

    Let me know if this feedback can be useful for you

    CREATE PROCEDURE [dbo].[spReopenClosed] 
    (
        @Return_Message VARCHAR(1024) = ''  OUT,        
        @IID        uniqueidentifier,
        @OpenDate   smalldatetime,
        @ReopenedBy uniqueidentifier
    )
    AS
    
        SET NOCOUNT ON;
    
    /******************************
    *  Variable Declarations
    *******************************/
        DECLARE     @ErrorCode  int  
        DECLARE     @ErrorStep  varchar(200)
    
    /******************************
    *  Initialize Variables
    *******************************/
    
        SELECT @ErrorCode = @@ERROR
    
    BEGIN TRY
    
        BEGIN TRAN
            /****************************************************************************
            * Step 1
            * Copy the Closed from the Archive
            ****************************************************************************/
    
            SELECT @ErrorStep = 'Error in Copying from the archive';
    
            INSERT INTO OPS.dbo.SM_T_In
            SELECT *         
            FROM OPS_ARCHIVE.Archive.SM_T_In
            WHERE GUID = @IID
                AND W.OpenDate = @OpenDate
    
    
            /****************************************************************************
            * Step 2
            * copy the notes
            ****************************************************************************/
    
            SELECT @ErrorStep = 'Error in copying the notes'
    
            INSERT INTO OPS.dbo.SM_T_Notes
            SELECT *
            FROM OPS_ARCHIVE.Archive.SM_T_Notes
            WHERE GUID = @IID
    
            /****************************************************************************
            * Step 3
            * Delete the from the Archive - this will also delete the notes
            ****************************************************************************/
    
            SELECT @ErrorStep = 'Error in deleting the items from the Archive'
    
            DELETE
            FROM OPS_ARCHIVE.Archive.SM_T_In
            WHERE OPS_ARCHIVE.Archive.SM_T_In.GUID = @IID
    
        COMMIT TRAN
    
        SELECT  @ErrorCode  = 0, @Return_Message = 'All data was moved over'
    
        /*************************************
        *  Return from the Stored Procedure
        *************************************/
        RETURN @ErrorCode                               -- =0 if success,  <>0 if failure
    
    END TRY
    
    BEGIN CATCH
        /*************************************
        *  Get the Error Message for @@Error
        *************************************/
        IF @@TRANCOUNT > 0 ROLLBACK
    
        SELECT @ErrorCode = ERROR_NUMBER()
            , @Return_Message = @ErrorStep + ' '
            + cast(ERROR_NUMBER() as varchar(20)) + ' line: '
            + cast(ERROR_LINE() as varchar(20)) + ' ' 
            + ERROR_MESSAGE() + ' > ' 
            + ERROR_PROCEDURE()
    
        /*************************************
        *  Return from the Stored Procedure
        *************************************/
        RETURN @ErrorCode                               -- =0 if success,  <>0 if failure
    
    END CATCH
    

提交回复
热议问题