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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:07:26+00:00 2026-05-15T00:07:26+00:00

I have a TSQL script that does a lot of database structure adjustments but

  • 0

I have a TSQL script that does a lot of database structure adjustments but it’s not really safe to just let it go through when something fails.

to make things clear:

  • using MS SQL 2005
  • it’s NOT a stored procedure, just a script file (.sql)

what I have is something in the following order

BEGIN TRANSACTION
    ALTER Stuff
    GO

    CREATE New Stuff
    GO

    DROP Old Stuff
    GO
IF @@ERROR != 0
    BEGIN
  PRINT 'Errors Found ... Rolling back'
  ROLLBACK TRANSACTION
  RETURN
    END
ELSE
     PRINT 'No Errors ... Committing changes'
     COMMIT TRANSACTION

just to illustrate what I’m working with … can’t go into specifics
now, the problem …

When I introduce an error (to test if things get rolled back), I get a statement that the ROLLBACK TRANSACTION could not find a corresponding BEGIN TRANSACTION.
This leads me to believe that something when REALLY wrong and the transaction was already killed.
what I also noticed is that the script didn’t fully quit on error and thus DID try to execute every statement after the error occured. (I noticed this when new tables showed up when I wasn’t expecting them because it should have rollbacked)

  • 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-15T00:07:27+00:00Added an answer on May 15, 2026 at 12:07 am

    When the error occurs, the transaction is rolled back automatically, and the current batch is aborted.

    Execution continues into the next batch, however. So all the stuff in the batches after the error gets executed. And then when you check for errors later, you try to rollback an already rolled back transaction.

    Also, to stop the entire script, not just the current batch, you should use:

    raiserror('Error description here', 20, -1) with log
    

    See my answer here for details on that one.

    So you need to check for @error after each batch, I think something like this should work:

    BEGIN TRANSACTION
    GO
    
    ALTER Stuff
    GO
    
    if @@error != 0 raiserror('Script failed', 20, -1) with log
    GO
    
    CREATE New Stuff
    GO
    
    if @@error != 0 raiserror('Script failed', 20, -1) with log
    GO
    
    DROP Old Stuff
    GO
    
    if @@error != 0 raiserror('Script failed', 20, -1) with log
    GO
    
    PRINT 'No Errors ... Committing changes'
    COMMIT TRANSACTION
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TSQL script that is used to set up a database as
Let's say I have to implement a piece of T-SQL code that must return
I have a SQL script that has to be run every time a client
I have a T-SQL statement that basically does an insert and OUTPUTs some of
I'm migrating a TSQL stored procedure to PL/SQL and have encountered a problem -
Are there any inherent problems with calling an ole object from tsql? I have
I have a T-SQL table variable (not a table) which has an auto incrementing
I have a table (T1) in t-sql with a column (C1) that contains almost
I have stored procedures in SQL Server T-SQL that are called from .NET within
Have just started using Google Chrome , and noticed in parts of our site,

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.