Sunday, February 19, 2012

how to stop executing a script

a setup process executes a script via osql. I want to parameterize
it in such a way that whenever wanted the script will do nothing.
For that I plan to created a table in master database, say
NODROP_DATABASE
the first few lines of the script will be
IF EXISTS (SELECT * FROM master.dbo.sysobjects where name =
'NODROP_DATABASE'
and type = 'U')
BEGIN
return
END
-- rest of the script here with many GO in between.
what is happening is that the return statement takes the control to the
first line after GO. The execution continues from the first GO after
that.
Is there any other approach to stop executing.Assuming the script is run by a SYSADMIN you can RAISERROR with a severity
of 20 or greater to terminate the current connection.
Alternatively, run the script from OSQL with the -b option and it will
terminate on the first error.
David Portas
SQL Server MVP
--|||... or, if using OSQL, issue a RAISERROR with state 127. OSQL will stop on
errors with state 127.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:peKdnWO5478MiE3fRVn-vw@.giganews.com...
> Assuming the script is run by a SYSADMIN you can RAISERROR with a severity
of 20 or greater to
> terminate the current connection.
> Alternatively, run the script from OSQL with the -b option and it will ter
minate on the first
> error.
> --
> David Portas
> SQL Server MVP
> --
>|||Tibor Karaszi wrote:

> ... or, if using OSQL, issue a RAISERROR with state 127. OSQL will stop on errors
with state 127.
David and Tibor,
Raising error with RAISEERROR means abnormal termination. The problem
with
that approach is that setup has to be programmed to ignore 127. I want
a graceful exit from running the script as if the script ran
successfully.|||Why not just add the conditional test in your calling code and decide
at that point whether or not to execute the script. I can't think of a
better way.
David Portas
SQL Server MVP
--|||Hi,
You could try to use labels.
IF (1 = 1)
GOTO QUIT
PRINT 'Not executing code'
QUIT:
PRINT 'Quit'
Ramon @. Havana Club,
SQL Server MCDBBA
"Data Cruncher" <dcruncher4@.netscape.net> '?/'' ? ''
'?: news:1121093312.487489.156320@.z14g2000cwz.googlegroups.com...
> Tibor Karaszi wrote:
>
on errors with state 127.[vbcol=seagreen]
> David and Tibor,
> Raising error with RAISEERROR means abnormal termination. The problem
> with
> that approach is that setup has to be programmed to ignore 127. I want
> a graceful exit from running the script as if the script ran
> successfully.
>

No comments:

Post a Comment