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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:25:59+00:00 2026-06-15T04:25:59+00:00

I have two similar TSQL scripts where I have SET XACT_ABORT ON. I expect

  • 0

I have two similar TSQL scripts where I have SET XACT_ABORT ON. I expect _test table to NOT EXIST after script execution as I am intentionally using a statement wrapped in a transaction that will raise an error.

The first script correctly rolls back the creation of _test table.

SET XACT_ABORT ON
GO
BEGIN TRANSACTION
CREATE TABLE dbo._test(ID int IDENTITY(1, 1) NOT NULL)
EXEC sp_does_not_exist --raises error!
GO
IF @@TRANCOUNT > 0
    COMMIT;

However, the second script does not roll back the creation of _test and the table exists after the script execution:

SET XACT_ABORT ON
GO
BEGIN TRANSACTION
CREATE TABLE dbo._test(ID int IDENTITY(1, 1) NOT NULL)
EXEC sp_recompile sp_does_not_exist; --raises error!
GO
IF @@TRANCOUNT > 0
    COMMIT;

Why does the second script not remove the _test table after execution?

  • 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-15T04:26:00+00:00Added an answer on June 15, 2026 at 4:26 am

    You haven’t mentioned your SQL Server version, but since the only difference in your script is sp_recompile, that seems like a good place to look. In 2008R2 it has the following logic:

    BEGIN TRANSACTION
    -- CHECK VALIDITY OF OBJECT NAME --
    --  (1) Must exist in current database
    --  (2) Must be a table or an executable object
    select @objid = object_id(@objname, 'local')
    
        if @objid is null OR
            (ObjectProperty(@objid, 'IsTable') = 0 AND
             ObjectProperty(@objid, 'IsExecuted') = 0)
        begin
            COMMIT TRANSACTION
            raiserror(15165,-1,-1 ,@objname)
            return @@error
        end
    

    So sp_recompile checks for the existence of the object before it tries to access it directly, and if it isn’t found it raises an error with severity -1. The documentation for RAISERROR states that a severity level less than zero is interpreted as zero, and the documentation for severity levels states that no system error is raised on severity zero.

    Indeed, adding the RAISERROR from sp_recompile into your script shows that it doesn’t affect @@TRANCOUNT:

    SET XACT_ABORT ON
    GO
    BEGIN TRANSACTION
    CREATE TABLE dbo._test(ID int IDENTITY(1, 1) NOT NULL)
    select @@trancount as 'before raiserror'    
    raiserror(15165,-1,-1 ,'sp_does_not_exist')
    select @@trancount as 'after raiserror' 
    
    GO
    IF @@TRANCOUNT > 0
        COMMIT;
    

    @@TRANCOUNT is 1 before and after the error is raised, so there is nothing to trigger a rollback. But if you do this, the second SELECT is never executed, because the error has been raised ‘directly’ by the database engine:

    SET XACT_ABORT ON
    GO
    BEGIN TRANSACTION
    CREATE TABLE dbo._test(ID int IDENTITY(1, 1) NOT NULL)
    select @@trancount as 'before raiserror'    
    exec sp_does_not_exist
    select @@trancount as 'after raiserror' 
    
    GO
    IF @@TRANCOUNT > 0
        COMMIT;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with similar columns - let's say table A with column
I have two click-events, that are nearly similar, but not quite. I am wondering
I have two almost similar source code trees, but do not have access to
tsql newbie here. I have a table, similar to this one: CarId CarName UserId
I have two similar questions How does php script executes. For example I have
I have two very similar usecases, one works, and another one does not. I
I have two similar table in database, for example: news1: id | title |
I have two very similar but not identical C# objects. I am copying the
I have two similar queries on same table and same where condition but different
I have two similar (not identical) dlls, one of them being actually a part

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.