GOTO SQL statement in multiple GO script

I need to exit the SQL script without error if a certain condition is met. I read that 1 solution will be raiseerrorwith an error code parameter of 20+ and with log. But the limitation is that I can only do this as an administrator, and the connection to db will be canceled.

In addition, I tried to use GOTO and go to the end of the script, but it does not work, because I have several GOs in the middle of the script. Is there any other solution?

IF <some condition> BEGIN
GOTO Finished;
END
GO

Finished:
SELECT 'Done'

Thank!

+3
source share
1 answer

gotocan't go through go. You will need to repeat the condition in each block:

IF NOT <some condition> 
BEGIN
   ...
END
GO
IF NOT <some condition> 
BEGIN
   ...
END
GO
IF NOT <some condition> 
...
+3
source

Source: https://habr.com/ru/post/1769034/


All Articles