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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:29:20+00:00 2026-06-05T07:29:20+00:00

If I am coding a SQL Server (2008r2) procedure, and I wrap it in

  • 0

If I am coding a SQL Server (2008r2) procedure, and I wrap it in a transaction, do I need to explicitly enclose it in a try..catch block, and then explicitly call rollback in the catch block, or will it exit and rollback the same on its own?

i.e.:

How does this:

    begin transaction

    begin try
    delete from....

    insert into...
    end try
    begin catch
    rollback transaction
    return
    end catch

    commit transaction

Compare with:

    begin transaction
    delete from....

    insert into...
    commit transaction

Thank you for any help.

  • 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-06-05T07:29:21+00:00Added an answer on June 5, 2026 at 7:29 am

    The answer to your question depends on the SET XACT_ABORT setting:

    Specifies whether SQL Server automatically rolls back the current
    transaction when a Transact-SQL statement raises a run-time error.

    When SET XACT_ABORT is ON, if a Transact-SQL statement raises a
    run-time error, the entire transaction is terminated and rolled back.

    When SET XACT_ABORT is OFF, in some cases only the Transact-SQL
    statement that raised the error is rolled back and the transaction
    continues processing. Depending upon the severity of the error, the
    entire transaction may be rolled back even when SET XACT_ABORT is OFF.
    OFF is the default setting.

    Compile errors, such as syntax errors, are not affected by SET
    XACT_ABORT.

    For example, try the following code. The first division by 0 raises an error but continues execution. The second division by zero raises an error and halts execution:

    begin transaction
    
    set xact_abort off
        
    select 1 / 0 -- causes divide by zero error, but continues
    select @@trancount -- returns 1
    
    set xact_abort on
    
    select 1 / 0 -- causes divide by zero error and terminates execution
    select @@trancount -- we never get here
    
    rollback
    

    If XACT_ABORT is ON, then errors will abort the transaction, and you don’t need a TRY / CATCH.

    If XACT_ABORT is OFF, you will need to check the status of each statement to see if an error occurred:

    begin transaction
    
    delete from...
    if @@error <> 0
    begin
        if @@trancount > 0
            rollback
        return
    end
    
    insert into...
    if @@error <> 0
    begin
        if @@trancount > 0
            rollback
        return
    end
    
    commit
    

    However, if you ever find a case where you need to TRY / CATCH, you may need to do something special when the error occurs. If so, don’t forget to TRY / CATCH the exception handling:

    begin transaction
    
    set xact_abort on
    
    begin try
        select 1 / 0 -- causes divide by zero error and terminates execution
        select @@trancount -- we never get here
        commit
    end try
    begin catch
        select xact_state() -- this will be -1 indicating you MUST rollback before doing any other operations
        select @@trancount -- this will probably be one, because we haven't ended the transaction yet
        if xact_state() <> 0
        begin try
            select 'rollback'
            rollback
            
            -- do something to handle or record the error before leaving the current scope
            select 'exception processing here'
            --insert into...
        end try
        begin catch
            -- ignore rollback errors
        end catch
        
    end catch
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL Server 2008 and VS 2008 Pro. And I am coding in
I am coding SQL Server 2005 trigger. I want to make some logging during
Background info: I'm coding with C#, using Microsoft SQL Server for databases. I didn't
None of my SQL Server stored procedure editing IDEs seem to have any tools
I have a ASP.NET web page that needs to make a SQL Server call
Server Version: SQL Server 2008R2 Client Version: SQL Server Express 2008R2 I have been
I want to synchronization with SQL server 2008 & sqlite.Coding wise ok.Everything is perfect.But
I am using SQL Server 2005. I was coding with a friend of mine
I am new to SQL server. I need to calculate the median of the
I need to write some code for renaming a column in SQL Server 2008.

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.