title: Exit and rollback everything in script on error
body: <p>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.</p>
  
  <p>to make things clear:</p>
  
  <ul>
  <li>using MS SQL 2005</li>
  <li>it's NOT a stored procedure, just a script file (.sql)</li>
  </ul>
  
  <p>what I have is something in the following order</p>
  
  <pre><code>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
  </code></pre>
  
  <p>just to illustrate what I'm working with ... can't go into specifics
  now, the problem ...</p>
  
  <p>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)</p>