if ( 1 = 1)
begin
print 'condition was true'
return --exit the script completely -- how do I do that?
end
-- otherwise continue with the script
print 'continue'
go
print 'with'
go
print 'the'
go
print 'script'
go
Please let me know if this needs further clarification and TIA..sqlster wrote:
> Is it possible in tsql ? How can I do the following?
> if ( 1 = 1)
> begin
> print 'condition was true'
> return --exit the script completely -- how do I do that?
> end
> -- otherwise continue with the script
> print 'continue'
> go
> print 'with'
> go
> print 'the'
> go
> print 'script'
> go
> Please let me know if this needs further clarification and TIA..
Strictly speaking GO isn't a TSQL statement. It's a batch separator
that tells the client to submit a batch of TSQL statements. So the
answer is that it isn't possible in TSQL, it is possible using the
client utilities. Unfortunately you didn't specify what you are using
to execute this script.
On the OSQL command line you can use the -b option to terminate when an
error occurs. Then just put a RAISERROR in your IF statement block.
If you are executing the script as a sy

RAISERROR with a severity level >= 20 to terminate the connection.
David Portas
SQL Server MVP
--|||No, each batch is separate. If you don't need the GO batch separators:
IF (1=1)
BEGIN
RAISERROR(condition was true',1,1) WITH NOWAIT
RETURN
END
PRINT 'continue'
PRINT 'with'
PRINT 'the'
PRINT 'script'
or
IF (1=1)
BEGIN
PRINT 'Condition was true'
GOTO QuitLabel
END
PRINT 'continue'
PRINT 'with'
PRINT 'the'
PRINT 'script'
QuitLabel:
"sqlster" <nospam@.nospam.com> wrote in message
news:5D6CD06D-134F-4BCF-8A35-C7526405C915@.microsoft.com...
> Is it possible in tsql ? How can I do the following?
> if ( 1 = 1)
> begin
> print 'condition was true'
> return --exit the script completely -- how do I do that?
> end
> -- otherwise continue with the script
> print 'continue'
> go
> print 'with'
> go
> print 'the'
> go
> print 'script'
> go
> Please let me know if this needs further clarification and TIA..|||> RAISERROR(condition was true',1,1) WITH NOWAIT
-- sorry,
RAISERROR('condition was true',1,1) WITH NOWAIT|||David, I am using sql query analyzer.
"David Portas" wrote:
> sqlster wrote:
> Strictly speaking GO isn't a TSQL statement. It's a batch separator
> that tells the client to submit a batch of TSQL statements. So the
> answer is that it isn't possible in TSQL, it is possible using the
> client utilities. Unfortunately you didn't specify what you are using
> to execute this script.
> On the OSQL command line you can use the -b option to terminate when an
> error occurs. Then just put a RAISERROR in your IF statement block.
> If you are executing the script as a sy

> RAISERROR with a severity level >= 20 to terminate the connection.
> --
> David Portas
> SQL Server MVP
> --
>|||Aaron,
I have whole bunch of create/drop database objects such as views, stored
procs, tables, functions etc. I am not sure what would be impact of removing
all the go statements.
In other words, is there any thing wrong with the following:
IF (1=1)
BEGIN
RAISERROR('condition was true,1,1) WITH NOWAIT
RETURN
END
drop myproc1
create myproc1
...
--go take out go statement
drop myproc1
create myproc1
...
--go take out go statement
drop mytable1
create mytable1
...
TIA...
"Aaron Bertrand [SQL Server MVP]" wrote:
> No, each batch is separate. If you don't need the GO batch separators:
> IF (1=1)
> BEGIN
> RAISERROR(condition was true',1,1) WITH NOWAIT
> RETURN
> END
> PRINT 'continue'
> PRINT 'with'
> PRINT 'the'
> PRINT 'script'
> or
> IF (1=1)
> BEGIN
> PRINT 'Condition was true'
> GOTO QuitLabel
> END
> PRINT 'continue'
> PRINT 'with'
> PRINT 'the'
> PRINT 'script'
> QuitLabel:
>
>
> "sqlster" <nospam@.nospam.com> wrote in message
> news:5D6CD06D-134F-4BCF-8A35-C7526405C915@.microsoft.com...
>
>|||> I have whole bunch of create/drop database objects such as views, stored
> procs, tables, functions etc. I am not sure what would be impact of
> removing
> all the go statements.
Yes, for example, CREATE PROCEDURE must be the first statement in a batch.
Various people deal with this kind of situation in various ways. The way
Management Studio prepares scripts for multiple objects without GO, IIRC, is
that it wraps the CREATE PROCEDURE in dynamic SQL, e.g. EXEC('CREATE
PROCEDURE dbo.whatever ...');
A|||Thank you all for the help...
"Aaron Bertrand [SQL Server MVP]" wrote:
> Yes, for example, CREATE PROCEDURE must be the first statement in a batch.
> Various people deal with this kind of situation in various ways. The way
> Management Studio prepares scripts for multiple objects without GO, IIRC,
is
> that it wraps the CREATE PROCEDURE in dynamic SQL, e.g. EXEC('CREATE
> PROCEDURE dbo.whatever ...');
> A
>
>sql
No comments:
Post a Comment