Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364

    Unanswered: 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 ?

    GW
    Last edited by GWilliy; 10-26-07 at 06:46.
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by GWilliy
    Is there a way to do this or am I missing something fundamental here ?
    No.

    -PatP

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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.
    George
    Home | Blog

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    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.

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    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;
    Code:
    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.

  6. #6
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364
    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
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •