Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:56:43+00:00 2026-05-22T21:56:43+00:00

I have never used a Transaction, Commit and Rollback before and now I need

  • 0

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 correctly but I am still not sure if I have coded this correct. I am hoping someone can review and advise me if this seems correct.

Basically I have 2 databases for an application. One is an archive – meaning data that is no longer going to be manipulated by the users will be moved to this DB. But in the event they ever need it, I will move the needed data back to the main database for use. My stored proc is below:

CREATE PROCEDURE [dbo].[spReopenClosed] 
(
    @Return_Message VARCHAR(1024) = ''  OUT,        
    @IID        uniqueidentifier,
    @OpenDate   smalldatetime,
    @ReopenedBy uniqueidentifier
)
AS
BEGIN
    SET NOCOUNT ON;

/******************************
*  Variable Declarations
*******************************/
    DECLARE     @ErrorCode  int  



/******************************
*  Initialize Variables
*******************************/

    SELECT @ErrorCode = @@ERROR

    IF @ErrorCode = 0

    BEGIN TRANSACTION
        /****************************************************************************
        * Step 1
        * Copy the Closed from the Archive
        ****************************************************************************/
        INSERT INTO OPS.dbo.SM_T_In
        SELECT          
        FROM OPS_ARCHIVE.Archive.SM_T_In    W
        WHERE W.GUID = @IID
            AND W.OpenDate = @OpenDate


        IF @ErrorCode <> 0
            BEGIN
                -- Rollback the Transaction
                ROLLBACK

                RAISERROR ('Error in Copying from the archive', 16, 1)
                RETURN
            END


        /****************************************************************************
        * Step 2
        * copy the notes
        ****************************************************************************/
        INSERT INTO OPS.dbo.SM_T_Notes
        SELECT 
        FROM OPS_ARCHIVE.Archive.SM_T_Notes W
        WHERE W.GUID = @IID

        IF @ErrorCode <> 0
            BEGIN
                -- Rollback the Transaction
                ROLLBACK

                RAISERROR ('Error in copying the notes', 16, 1)
                RETURN
            END

        /****************************************************************************
        * Step 3
        * Delete the from the Archive - this will also delete the notes
        ****************************************************************************/
        DELETE
        FROM OPS_ARCHIVE.Archive.SM_T_In
        WHERE OPS_ARCHIVE.Archive.SM_T_In.GUID = @IID

        IF @ErrorCode <> 0
            BEGIN
                -- Rollback the Transaction
                ROLLBACK

                RAISERROR ('Error in deleting the items from the Archive', 16, 1)
                RETURN
            END

        COMMIT

        BEGIN
            SELECT  @ErrorCode  = @@ERROR

            IF @ErrorCode = 0
                    SELECT  @Return_Message = 'All data was moved over'
        END



/*************************************
*  Get the Error Message for @@Error
*************************************/
    IF @ErrorCode <> 0
    BEGIN
        SELECT  @Return_Message = [Description]     -- Return the SQL Server error
          FROM  master.dbo.SYSMESSAGES
         WHERE  error = @ErrorCode
    END

/*************************************
*  Return from the Stored Procedure
*************************************/
    RETURN @ErrorCode                               -- =0 if success,  <>0 if failure
END

I have two inserts that move the data from 2 tables from the Archive database. If those inserts are successful, then I will delete the data from the Archive DB. I would appreciate any feedback on this, I need to make sure that I am doing this properly.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:56:44+00:00Added an answer on May 22, 2026 at 9:56 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used Prototype before. But now when I'm using Rails, it seems
I have never used octal numbers in my code nor come across any code
i have never used LINQ in any of my projects , i have always
I have never used virtualization, and am trying to get up to speed. My
I've never used the Global Temporary Tables however I have some questions how they
I have used automation to insert values into a cell, however I have never
I have often heard this term being used, but I have never really understood
I'm used to doing Java programming, where you never really have to think about
I have seen transaction usage in some cases but never really understood in which
I have never used ClearCase, but have used Subversion and for a short period

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.