3

I need to exit a SQL script without an error if a certain condition holds true. I've read that 1 solution would be to raiseerror with error code 20+ and with log parameter. But the limitation for that is that i can execute that only as an admin and the connection to the db will be aborted.

Also, I tried using GOTO and jump to the end-of-the-script, but it doesnt work, because I have multiple GO in the middle of the script. Is there a another solution?

IF <some condition> BEGIN
GOTO Finished;
END
GO

Finished:
SELECT 'Done'

Thanks!

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
kateroh
  • 4,382
  • 6
  • 43
  • 62

1 Answers1

4

goto cannot jump past a go. You'd have to retest the condition in each block:

IF NOT <some condition> 
BEGIN
   ...
END
GO
IF NOT <some condition> 
BEGIN
   ...
END
GO
IF NOT <some condition> 
...
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 1
    +1, it is either this or use `EXEC(@SQL)` for this things that need the GO, and then you can use the `GOTO`. – KM. Oct 11 '10 at 20:38