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

  • SEARCH
  • Home
  • 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 6957385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:59:40+00:00 2026-05-27T14:59:40+00:00

So we have multiple stored procs that get used during a morning SQL job

  • 0

So we have multiple stored procs that get used during a morning SQL job and the procs get called in sequence. In the event one of the queries inside a proc fails, we have an error catching/logging that we do for each query so we know exactly what piece failed. But the problem is that some of these processes are very difficult to start over if something failed so I am thinking of implementing a TRANSACTION in each stored proc.

The current procedure is similar to this:

CREATE PROCEDURE [dbo].[spStep01]
( 
    @Return_Message     Varchar(1024)   OUT     -- Error messages returned to the calling program
)
AS
BEGIN

SET NOCOUNT ON;

DECLARE @ErrorCode              int
DECLARE @ApplicationNumber      int             
DECLARE @TaskNumber             int             
DECLARE @TaskCompleted          smallint        
DECLARE @TaskFailed             smallint        
DECLARE @TaskRunning            smallint        
DECLARE @ErrorSeverity          smallint        
DECLARE @ErrorState             smallint        

SELECT @ErrorCode = @@ERROR
SELECT @ApplicationNumber   = 10                
SELECT @TaskNumber          = 1
SELECT @TaskCompleted       = 0
SELECT @TaskFailed          = -1
SELECT @TaskRunning         = 1
SELECT @ErrorSeverity       = 16
SELECT @ErrorState          = 1

/***************************************************************************
*  first insert
***************************************************************************/
    BEGIN TRY   
        INSERT INTO ...
    END TRY
    BEGIN CATCH
        SELECT @Return_Message = 'FAILED - first insert did not populate'
        EXEC dbo.spTrackTask '', @ApplicationNumber, @TaskNumber, @TaskFailed, @Return_Message      
        RAISERROR (@Return_Message, @ErrorSeverity, @ErrorState)
        RETURN 
    END CATCH

/***************************************************************************
*  second insert
***************************************************************************/
    BEGIN TRY   
        INSERT INTO ...
    END TRY
    BEGIN CATCH
        SELECT @Return_Message = 'FAILED -  second insert did not populate'
        EXEC dbo.spTrackTask '', @ApplicationNumber, @TaskNumber, @TaskFailed, @Return_Message      
        RAISERROR (@Return_Message, @ErrorSeverity, @ErrorState)
        RETURN 
    END CATCH


/***************************************************************************
* Procedure has completed successfully
***************************************************************************/
    SELECT @Return_Message = 'SUCCESS - Inserts were complete'
    EXEC dbo.spTrackTask '', @ApplicationNumber, @TaskNumber, @TaskCompleted, @Return_Message   


/*************************************
*  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

What I am trying to determine is if I wrap all of the TRY/CATCH blocks in a TRANSACTION and an error is raised if it will rollback everything. I looked around on SO and found a few examples of one TRY/CATCH block but we would have multiple in most of the stored procs. I don’t have much experience with transactions so I am not 100% sure how to implement it correctly in this case.

Will wrapping it in a TRANSACTION work? Or is there a better way to do this?

  • 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-27T14:59:41+00:00Added an answer on May 27, 2026 at 2:59 pm

    How about this

    BEGIN TRANSACTION tx
    BEGIN TRY
      @CurrentStep = "First Insert"
      INSERT ...
    
      @CurrentStep = "Second Insert"
      INSERT ...Second
    
      IF @@TRANCOUNT > 0
      BEGIN --SUCCESS, nothing failed, now I can commit!!
        SELECT @Return_Message = 'SUCCESS - Inserts were complete'
        EXEC dbo.spTrackTask '', @ApplicationNumber, @TaskNumber, @TaskCompleted, @Return_Message   
        COMMIT TRANSACTION tx;    -- now everything is committed
      END
    END TRY
    
    BEGIN CATCH 
      IF @@TRANCOUNT > 0 --something failed
      BEGIN
        IF @CurrentStep = 'First Insert'
          SELECT @Return_Message = 'FAILED - first insert did not populate'
        ELSE IF @CurrentStep = 'Second Insert'
          SELECT @Return_Message = 'FAILED - second insert did not populate'
    
        EXEC dbo.spTrackTask '', @ApplicationNumber, @TaskNumber, @TaskFailed, @Return_Message      
        RAISERROR (@Return_Message, @ErrorSeverity, @ErrorState)
    
        ROLLBACK TRAN tx; -- everything is rolled back
      END
    END CATCH
    

    Hope this does the trick for you, remember that everything inside a transaction should be committed or rolled back in one place, otherwise you’re better off having multiple transactions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in one of my Sql (2008) Stored Procs which
I have a SQL stored procedure that will return only one record. However, in
I have a stored procedure that returns multiple tables. How can I execute and
i have a stored procedure that has to retrieve data from multiple tables something
Is there any way to use Dapper.NET with stored procs that return multiple result
Basically, I have multiple URL's stored in a MySQL table. I want to pull
I have multiple xaml based pages stored as children of a canvas on another
Which command will executed first,If a stored procedure have individual multiple select commands;
I have a stored procedure with multiple insert/select statements. Let's say I'm using the
I love NHibernate's ability to have one table to store multiple types based on

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.