Sunday, February 26, 2012

EXIT Installation Scripts that have multiple GO statements

Hi All

I'm Scripting various Upgrade scripts that set up roles / users / create databases etc and want to Exit the Script unconditionally if for example the user already exists etc.

I can't Use RETURN because it just jumps to the Code following the next GO statement

There is also some USE [Database] Statements

Is there a way to do this or am I missing something fundamental here ?

GWIs there a way to do this or am I missing something fundamental here ?No.

-PatP|||I think conditional statements are the way to go...

If the username exists, do nothing, else do something...
OR
If the username doesn't exist, do something.|||What if he dumped the monolithic type script and removed the batch separators instead calling a series of individual files in series? Sean posted a script to do this recently and I have a similar one but using sqlcmd.

George - I think the problem is that batch separators are required as otherwise the deferred name resolution causes the script to fail as dependant objects do not exist at compile time. Also, some objects (e.g. functions & procs) can be the only statements in a batch.|||this can be done BUT the script must always be run by either SQLCMD or maybe OSQL. Here is a sampling of the file that you would use;

select count(*)
from sys.databases
go

if exists (select * from sys.tables)
begin
exit
end

go

select count(*)
from sys.databases
go

This will always generate an error in Query Analyzer, but SQLCMD will exit as you desire.

EDIT: Note; when you test this script, you only get the results of the first query. The second query is ignored.|||Folks

Thanks for all your input & Apologies for not getting back to you sooner.

In this instance the new system is still in Dev & I'm releasing full versions into UAT / Testing environments (which comprise 2 new interface databases, Linked Servers & multiple security accounts setup) - hence the monolothic script.

I think I'll have to accept Pat's Answer.

Thanks again

GW

No comments:

Post a Comment