Wednesday, March 7, 2012

Don't Run Script If Database Exists

Hi there
I want to add a bit to my SQL script which will check whether the
database exists before running the script. I am aware of the syntax for
checking for the database and dropping the database if necessary, but I
want to keep the database if it exists. The trouble is that because I
need GO statements in the script, I cannot put an IF EXISTS around the
whole thing?
Would anyone be able to help?
Thanks
DaveIf you run the script using ISQL or OSQL, you can issue a RAISERROR with *st
ate* of 127, which will
terminate the script (not only the batch). If you run from somewhere else, y
ou can use the technique
that instpubs.sql does, i.e., issue an error with a high *severity* level so
that *SQL Server*
terminates the connection.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"Bungle" <dave@.court-technologies.com> wrote in message
news:1106556674.118686.293460@.f14g2000cwb.googlegroups.com...
> Hi there
> I want to add a bit to my SQL script which will check whether the
> database exists before running the script. I am aware of the syntax for
> checking for the database and dropping the database if necessary, but I
> want to keep the database if it exists. The trouble is that because I
> need GO statements in the script, I cannot put an IF EXISTS around the
> whole thing?
> Would anyone be able to help?
> Thanks
> Dave
>

No comments:

Post a Comment