34

This seems like an incredibly dumb question to have to ask, but how do I get SQL Server Management Studio to stop processing a SQL script when it encounters an error?

I have a long script and if there is an error at the start SSMS reports it and then blindly carries on, screwing up things even more. I can't use transactions because the script creates several databases and they can't go in a transaction. It is the database creation that sometimes fails.

Toad for SQL Server will pause at the first error it encounters and ask if you want to stop or continue. This is the behaviour I'm looking for. Does it exist in SSMS?

I am not asking, "How do I write or modify a script so that it stops on an error?" I'm not interested in modifying my script to make this happen, I just want SSMS to stop on an error. Toad for SQL Server does exactly this and that is the behaviour I want. This is also not a duplicate of 659188 because that relates to modifying the script to stop SSMS.

4
  • It's blindly doing what you told it to do! Did you tell it to stop on an error? Would the same script stop on error if run from ADO.NET or from some other script execution tool? The problem is your script, not SSMS. May 11, 2009 at 0:07
  • 8
    Most modern high-level execution environments immediately halt on error. Unless explicitly told not to halt or told to branch to error-handling statements on error.
    – yfeldblum
    May 11, 2009 at 0:11
  • Assuming that's true (and if I cared, I'd ask you to back that up), what's it got to do with the question, which is about SQL Server. The semantics of a batch of T-SQL Statements don't include stopping on error without being told to do so. These semantics don't change simply because "most modern high-level execution environments immediately halt on error". May 11, 2009 at 0:15
  • 17
    None of these comments are helpful guys. He's just asking where the settings are that would produce this kind of behavior. May 11, 2009 at 0:18

9 Answers 9

11

Short answer: You can't.

Thanks to those that provided workarounds, but it seems that SSMS itself can not be set to pause or stop on an error in the same way that Toad for SQL Server can.

3
  • 1
    I don't think this is true. It's not the tool that's supposed to handle this, it's the script. You need to use TRY CATCH blocks or IF @@ERROR statements
    – Jeremy
    Jul 6, 2009 at 23:06
  • do not use go statements, just enter the statements one after another Aug 19, 2009 at 22:43
  • @BlackTigerX The go's may be needed for things like alter view which must be first statement of a block.
    – crokusek
    May 24, 2017 at 0:22
7

consider using the command line program 'sqlcmd' that comes with SQL Server, with the -b and the -V options set. -b will cause sqlcmd to quit when it hits an error. -V controls the severity level that is considered to be an error.

7

11 years later SSMS still doesn't have this feature...

BUT! You can enable SQLCMD mode (Menu/Query/SQLCMD Mode) and then in text editor you can define this option:

:ON ERROR EXIT

before your t-sql script. Now it will stop execution on error.

1
6

ApexSQL Script generates batch scripts in exactly the manner you want. As an example:

--Script Header
begin transaction
go

{Statement #1}
go
--Standard Post-Statement Block
if @@error <> 0 or @@trancount = 0 begin
    if @@trancount > 0 rollback transaction
    set noexec on
end
go

{Statement #2}
go
--Standard Post-Statement Block
if @@error <> 0 or @@trancount = 0 begin
    if @@trancount > 0 rollback transaction
    set noexec on
end
go

--Script Footer
if @@trancount > 0 commit transaction
go
set noexec off
go
5

You need to wrap your SQL Statements inside a Transaction.

BEGIN TRANSACTION
   /* run all your SQL statements */
COMMIT TRANSACTION

If there's an error inside the begin/end transaction, all statements will be rolled back.

EDIT: Wrapping inside inside begin/end transaction, will prevent the statements from getting committed to the database, but not stop it at that point. You need to additionally wrap it inside a try/catch block as follows:

BEGIN TRY
  BEGIN TRANSACTION
  /* run all your SQL statements */
  COMMIT TRANSACTION
END TRY
BEGIN CATCH
  ROLLBACK TRANSACTION
END CATCH
7
  • 1
    I can't. The script creates several databases and they can't go in a transaction and it is the database creation that sometimes fails.
    – TallGuy
    May 11, 2009 at 0:09
  • 1
    In that case, then the TRY/CATCH block should be enough May 11, 2009 at 0:14
  • 6
    In case of try/catch you can't use GO. Dec 12, 2011 at 16:23
  • Also any error during a transaction will cancel it but not rollback so you will get one error message more, afaik Dec 12, 2011 at 16:24
  • Might not be the right answer to the question but it was the answer I was looking for ;-)
    – gnuchu
    Sep 4, 2013 at 16:48
3

Wow. That's kinda rubbish isn't it? I use SQL-Workbench which, like Toad for SQL Server, handles this easily. Unlike Toad for SQL Server though, it's free.

I'm astonished that such fundamental functionality isn't part of the standard tool.

1
  • Toad also has a free version, up to N (5?) users per organization.
    – crokusek
    May 24, 2017 at 0:35
2

would using a try catch block help here. On error the try will be exited, implement error handling in the catch

http://msdn.microsoft.com/en-us/library/ms179296.aspx

2

There are a few more work-arounds mentioned here:

SQL Server - stop or break execution of a SQL script

and

SQL Server: How to abort a series of batches in Query Analyzer?

(raiseerror 20 with log, set noexec on, sqlcmd mode :on error exit, etc.)

0

If you can't put your script into a stored procedure and use the return statement to exit on error, the solution provided by @Justice might be your best bet. Everyone else is missing the point - you can't return from a script, even if you use transactions or even if you raiserror. SSMS will just execute the next thing anyway, even if set xact abort is on.

If you can convert your script to a stored procedure, then you can just return from it when you detect an error.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.