all groups > sql server programming > april 2007 >
You're in the

sql server programming

group:

Skip to end of SQL Script


Skip to end of SQL Script Michael Tissington
4/21/2007 7:39:26 PM
sql server programming: I have a large SQL script file with lots of batch blocks (ending in GO).

The first statement determine if the rest of the script should run using an
IF NOT EXIST

How do I skip to the end of the script file (or abort the script file) ?

Re: Skip to end of SQL Script M A Srinivas
4/21/2007 9:16:39 PM
On Apr 22, 7:39 am, "Michael Tissington"
[quoted text, click to view]

This may be one of the ways . Others may post better ways .
Trick is create a dummy table with a column not null . When you want
to abort the job insert null value to dummy table
with set xact_abort on
Example

set xact_abort on
begin tran
create table #a (keyid1 int, keyid2 int )
insert into #a values (1,2)
select * from #a
create table #dummy (keyid int not null)
go

if not exists (select 1 from #a where keyid1 = keyid2 )
begin
print 'keyd1<>keyid2'
insert into #dummy values(null)
end

select keyid1 from #a
select keyid2 from #a


go

select keyid1 from #a
go




if @@trancount > 0
commit
Re: Skip to end of SQL Script Tibor Karaszi
4/22/2007 12:00:00 AM
IF you execute the script using OSQL or SQLCMD, you can RAISERROR with a state of 127. These tools
will terminate the script on such an error.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi


[quoted text, click to view]
RE: Skip to end of SQL Script Bob
4/23/2007 2:52:04 AM
I've been using SQLCMD mode from Management Studio(SSMS). Throw SSMS into
SQLCMD mode by clicking 'Query' on the main menu, then 'SQLCMD Mode'.
Then paste this in. You'll know if you're in SQLCMD mode because the lines
starting with ':' will be highlighted in grey. Now run this script.
The ':exit' command behaves like a STOP.

Hope that helps

wBob

:on error exit
PRINT 'This should execute'
GO
RAISERROR( 'dummy error', 16, 1 )
GO
PRINT 'This won''t execute as long as :on error exit is at the top'
GO

:exit

PRINT 'This will NEVER execute'


[quoted text, click to view]
AddThis Social Bookmark Button