SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


error handling


error handling

Author
Message
ugo boy
ugo boy
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 27
Hello,
Can some one help me with a way that I can stop scripts from running if one of the scripts fail.

I have one sql script that will run a number of sql scripts using the r command
how ever I need to know if there is another way to stop the rest of the scripts from running if one fails.
I currently have something like this:

I created on script sbuildn.sql. this script is as follows:

SET NOCOUNT ON
GO

:On Error exit

PRINT ' '
PRINT 'Starting: Initialization script'
:r "P:\IMP\ini1.sql"
GO

PRINT ' '
PRINT 'Starting: Platform build'
:r "P:\IMP\ini2.sql"
GO

from the command prompt, I run:
SQLCMD -S Devlair-db -i "sbuildn.sql"

Is there another way to run this script so that an error in one will stop the execution of any further script?
Any help will be highly appreciated
Ugo
JohnnyDBA
JohnnyDBA
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1450 Visits: 897
The issue is, you are specifying the GO batch separator so if you encounter an error in one batch, the subsequent batches will still execute.

If you encounter an error you can try raising an error with severity 20. This will end the current connection completely, preventing subsequent batches from running. You must specify the WITH LOG option at the end...

RAISERROR('Script Failed!', 20, 1) WITH LOG



See how that works for you.

John
ugo boy
ugo boy
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 27
Thank you,
I added the
RAISERROR('Script Failed!', 20, 1) WITH LOG
and it closed the connection preventing subsequent scripts from running!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search