Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop execution of sql script

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

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

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

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

like image 829
Random User Avatar asked Sep 24 '15 13:09

Random User


1 Answers

There are no good solutions to this problem. One way you can share data between batches is tables(persistent or temporary). So you can have some logic that depends on some state of that table or value of particular columns in that table like this:

--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 to every place in script that you want to skip and depending on the fact that the table has some rows or not those code blocks will execute or not.

like image 86
Giorgi Nakeuri Avatar answered Oct 21 '22 17:10

Giorgi Nakeuri