Breaking out of a .sql batch script

  • Suppose I have the following simple statements in a batch script that I execute within either a mgmt studio context or something similar:

    select * from tbl

    go

    if not exists(select * from othertbl)

    --need to break out of the script here so the nothing else executes in the file

    go

    insert into tbl1(fld1)

    values(val1)

    go

    What I need is a way to break out of the script altogether without anything else being executed. The GO statements are autogenerated and cannot be removed so a RETURN statement doesn't work here....

  • Why do you have the "go"s in there?

    Why not:

    select * from tbl

    if not exists(select * from othertbl)

    insert into tbl1(fld1)

    values(val1)

    else

    select 'done'

  • The script I am using is extremely large (the build script generated by Visual Studio Database Developers Edition) and the go statements need to be in there for items where a CREATE must be the first statement in the execution.

    I have a conditional at the very top of the script so that if certain criteria aren't met, it needs to stop executing the .sql script.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply