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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:57:28+00:00 2026-05-13T20:57:28+00:00

So I just saw a weird behavior In one script there is something like:

  • 0

So I just saw a weird behavior

In one script there is something like:

  begin transaction
  begin try

       stuff
       stuff
       stuff

       print 'commit'
       commit transaction
  end try
  begin catch
       print 'rollback'
       print error_message()
       rollback transaction
  end catch

thing is when this script in run, I see the print commit message but it does not make the commit and lock the tables/rows/etc

I have to manually run a commit by selecting the line and run it.

but if I do this

 begin transaction
  begin try

       stuff
       stuff
       stuff

       commit transaction
       print 'commit'
  end try
  begin catch
       print error_message()
       rollback transaction
       print 'rollback'
  end catch

(swapped the print and the commit)

it work fine.

anyone know why this would happen?

  • 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-13T20:57:28+00:00Added an answer on May 13, 2026 at 8:57 pm

    this works fine for me:

    --create table t (rowid int)  --create one time before running script
    
    begin transaction
      begin try
    
           insert into t values (1)
           print 'commit'
           print XACT_STATE()   --should be 1
           commit transaction
           print XACT_STATE()   --should be 0
      end try
      begin catch
           print ERROR_MESSAGE()
           rollback transaction
           print 'rollback'
      end catch
    
    select * from t
    

    output

    commit
    1
    0
    rowid
    -----------
    1
    

    Close your SSMS window, open a new window, and then run your 1st script again, I’ll bet you had an open transaction the first time you ran it, so you needed that extra COMMIT.

    EDIT after OP comment:

    run this exact script in a new connection to each database:

    BEGIN TRY create table t (rowid int) END TRY BEGIN CATCH END CATCH
    
    print 'A - XACT_STATE()='+ISNULL(CONVERT(varchar(10),XACT_STATE()),'')+', @@TRANCOUNT='+ISNULL(CONVERT(varchar(10),@@TRANCOUNT),'')
    
    begin transaction
      begin try
    
           insert into t values (1)
           print 'commit'
           print 'B - XACT_STATE()='+ISNULL(CONVERT(varchar(10),XACT_STATE()),'')+', @@TRANCOUNT='+ISNULL(CONVERT(varchar(10),@@TRANCOUNT),'')
           commit transaction
           print 'C - XACT_STATE()='+ISNULL(CONVERT(varchar(10),XACT_STATE()),'')+', @@TRANCOUNT='+ISNULL(CONVERT(varchar(10),@@TRANCOUNT),'')
      end try
      begin catch
           print ERROR_MESSAGE()
           rollback transaction
           print 'rollback'
      end catch
    
    print 'D - XACT_STATE()='+ISNULL(CONVERT(varchar(10),XACT_STATE()),'')+', @@TRANCOUNT='+ISNULL(CONVERT(varchar(10),@@TRANCOUNT),'')
    
    select * from t
    

    you should get this:

    A - XACT_STATE()=0, @@TRANCOUNT=0
    
    (1 row(s) affected)
    commit
    B - XACT_STATE()=1, @@TRANCOUNT=1
    C - XACT_STATE()=0, @@TRANCOUNT=0
    D - XACT_STATE()=0, @@TRANCOUNT=0
    rowid
    -----------
    1
    
    (1 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.