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 6537701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:38:55+00:00 2026-05-25T10:38:55+00:00

I have a SQL Server 2008 R2 stored procedure that runs a few INSERTs

  • 0

I have a SQL Server 2008 R2 stored procedure that runs a few INSERTs and UPDATEs in a TRANSACTION. After each statement, I need to log what just happened before doing the next step.

Here is my code:

BEGIN TRY

    BEGIN TRANSACTION

        INSERT INTO... -- 1st statement
        INSERT INTO MyEventLog (EventDescription) VALUES ('Did Step 1') -- log

        UPDATE... -- 2nd statement
        INSERT INTO MyEventLog (EventDescription) VALUES ('Did Step 2') -- log

    COMMIT TRANSACTION

END TRY

BEGIN CATCH
    IF (@@TRANCOUNT<>0) ROLLBACK TRANSACTION
    EXEC LogError 'I got an error'
END CATCH

Problem is: if there is an error, the transaction rolls back all statements — including the logging which I need. in the event of an error, how do I roll back the transactions but keep the logging.

  • 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-25T10:38:55+00:00Added an answer on May 25, 2026 at 10:38 am

    I was going to ask why you would want to log an event that technically didn’t happen, since the transaction would have been rolled back and the database would be in the state it was in before the transaction. But then it occurred to me that you probably just want to log it in order to know WHERE it failed so you can fix the underlying issue, which is a smart thing to do.

    If that is indeed the case, the best thing to do is to rollback the entire transaction as you are currently doing, and to use your LogError SP to log the error message in another table. This is what I use:

    CREATE PROCEDURE [dbo].[Error_Handler]
    @returnMessage bit = 'False'
    WITH EXEC AS CALLER
    AS
    BEGIN
    
      DECLARE @number int,
        @severity int,
        @state int,
        @procedure varchar(100),
        @line int,
        @message varchar(4000)
    
      INSERT INTO Errors (Number,Severity,State,[Procedure],Line,[Message])
      VALUES (
        ERROR_NUMBER(),
        ERROR_SEVERITY(),
        ERROR_STATE(),
        isnull(ERROR_PROCEDURE(),'Ad-Hoc Query'),
        isnull(ERROR_LINE(),0),
        ERROR_MESSAGE())
    
      IF(@returnMessage = 'True')
      BEGIN
        select *
        from Errors
        where ErrorID = scope_identity()
      END
    END
    

    The error message should let you know what went wrong in what table, and that should be enough info to fix the problem.

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

Sidebar

Related Questions

I have a SQL Server 2008 stored procedure that updates values in a table.
I have a SQL Server 2008 database. This database has a stored procedure that
I have a stored procedure in SQL Server 2008 that will insert a record
I am currently using MS SQL Server 2008. I have a stored procedure that
I have a stored procedure that retrieves sensitive information from an SQL Server 2008
I have a stored procedure in SQL Server 2008 that consists of multple select
Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs
I have a SQL Server 2008 stored procedure which does a select count(*) ...
Does SQL Server 2008 have a built in debugger? I've got a stored procedure
I have a stored procedure in a SQL Server 2008 database. This stored procedure

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.