Stop sql script execution

I have a huge SQL script with many lots (using GO ).

In certain conditions, I want to stop the execution of this script.

I tried using NOEXEC , but I keep getting invalid object errors since my script involves creating and adding data to new tables and columns.

I do not want to use RAISERROR , is there any other way to do this?

+5
source share
1 answer

There are no good solutions to this problem. One way to share data between parties is a table (permanent or temporary). So you can have some logic that depends on some state of this table or the value of specific columns in this table:

 --CREATE TABLE debug(col int) --INSERT INTO debug VALUES(1) --DELETE FROM debug SELECT 1 GO SELECT 2 SELECT 3 GO IF EXISTS(SELECT * FROM debug) SELECT 6 GO 

Just add IF EXISTS(SELECT * FROM debug) this line anywhere in the script you want to skip, and depending on whether the table has multiple lines or these code blocks will not execute.

+4
source

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


All Articles