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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:54:51+00:00 2026-05-21T11:54:51+00:00

I would like to get some clarification to this answer -> Nested stored procedures

  • 0

I would like to get some clarification to this answer -> Nested stored procedures containing TRY CATCH ROLLBACK pattern?

Below is the code sample from the above link

 1 CREATE PROCEDURE [Name]
 2 AS
 3 SET XACT_ABORT, NOCOUNT ON
 4
 5 DECLARE @starttrancount int
 6
 7 BEGIN TRY
 8    SELECT @starttrancount = @@TRANCOUNT
 9
10    IF @starttrancount = 0
11        BEGIN TRANSACTION
12
13       [...Perform work, call nested procedures...]
14
15    IF @starttrancount = 0 
16        COMMIT TRANSACTION
17 END TRY
19 BEGIN CATCH
20    IF XACT_STATE() <> 0 AND @starttrancount = 0 
21        ROLLBACK TRANSACTION
22    RAISERROR [rethrow caught error using @ErrorNumber, @ErrorMessage, etc]
23 END CATCH
24 GO

Suppose it is called from the legacy stored procedure that starts a transaction.
In this case this proc will not start transaction of its own, but will alter the XACT_ABORT state for the call.

So, I have a few questions here.

  • Is XACT_ABORT active for the current
    proc only, or for the whole call
    stack?
  • If I want to refactor a proc to use
    SET XACT_ABORT ON, do I need to pair
    it up with SET XACT_ABORT OFF? Is this the safest way to do it for legacy code?

Below is the modified sample that turns on XACT_ABORT conditionally and pairs it up with turning it off on a proc exit

CREATE PROCEDURE [Name]
AS
SET NOCOUNT ON

DECLARE @starttrancount int

BEGIN TRY
   SELECT @starttrancount = @@TRANCOUNT

   IF @starttrancount = 0
   BEGIN
      SET XACT_ABORT ON
      BEGIN TRANSACTION
   END

      [...Perform work, call nested procedures...]

   IF @starttrancount = 0 
   BEGIN
      COMMIT TRANSACTION
      SET XACT_ABORT OFF
   END
END TRY
BEGIN CATCH
   IF XACT_STATE() <> 0 AND @starttrancount = 0 
   BEGIN
       ROLLBACK TRANSACTION
       SET XACT_ABORT OFF
   END
   RAISERROR [rethrow caught error using @ErrorNumber, @ErrorMessage, etc]
END CATCH
GO
  • 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-21T11:54:51+00:00Added an answer on May 21, 2026 at 11:54 am

    As far as I can see when execution of a child batch ends it restores the previous value of this setting.

    SET NOCOUNT ON;
    
    BEGIN TRAN
    
    SELECT CASE
             WHEN 16384 & @@OPTIONS = 16384
               THEN 'XACT_ABORT IS ON'
             ELSE ' XACT_ABORT IS OFF'
           END
    
    CREATE TABLE #T
      (
         C INT
      )
    
    SET XACT_ABORT OFF
    
    INSERT INTO #T
    VALUES      (1)
    
    EXEC ('SET XACT_ABORT ON; 
          INSERT INTO #T VALUES(2);
            SELECT CASE
                        WHEN 16384 & @@OPTIONS = 16384
                        THEN ''XACT_ABORT IS ON''
                        ELSE '' XACT_ABORT IS OFF''
                    END 
          ')
    
    
    INSERT INTO #T
    VALUES      (1 / 0)
    
    /*If XACT_ABORT was on we would never get here but we do!*/
    COMMIT
    
    SELECT *
    FROM   #T
    
    DROP TABLE #T
    
    SELECT CASE
             WHEN 16384 & @@OPTIONS = 16384
               THEN 'XACT_ABORT IS ON'
             ELSE ' XACT_ABORT IS OFF'
           END 
    

    Returns

    ------------------
     XACT_ABORT IS OFF
    
    
    ------------------
    XACT_ABORT IS ON
    
    Msg 8134, Level 16, State 1, Line 31
    Divide by zero error encountered.
    The statement has been terminated.
    C
    -----------
    1
    2
    
    
    ------------------
     XACT_ABORT IS OFF
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get some clarification regarding lazy loading and session boundaries etc.
I would like to get some nested params. I have an Order that has
I'm new on this, and I would like to get some advice because I
I would like to get some information on list of points that needs to
I would like to get some feedback on what is one of my first
What I would like to get some input on is how to remove certain
I have a number of links which I would like to get some information
I would like to get contents of some stream or project I can find
I would like to get to know how to redirect output of some program
Given some arbitrary SQL I would like to get the data types of the

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.