Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Intentionally terminating Job on step - HOW?

Author  Topic 

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2004-07-20 : 04:47:24
Couldn't think of how to phrase the topic

So - what I am talking about, briefly, is: How do I get a 3 step job, scheduled via SQL Server Agent, to end reporting an error, INTENTIONALLY - i.e how can I cause the behaviour.

For example:

Let's say I want to:
step (1) : create a new directory
step (2) : copy files to new directory
step (3) : delete an old direcory
step (4) : process files from the "new" directory.

Now, if steps 3 fails, I want to know about it, and will use a slightly modified version of the SP_SMTPMAIL proc to let me know. But the job should continue.
IF Step (1) fails, I need to stop, reporting failure. Ditto for steps 2 and 4.

I wrote a little code (stop laughing, dernit ), to create my directory:

if exists (select 'SP exists' from sysobjects where name = 'usp_create_folder' and xtype = 'p')
drop proc usp_create_folder
go
set nocount on
go
create procedure usp_create_folder @foldnm varchar(250)
as
BEGIN
-- Create By: Regan Galbraith
-- Create On: 2004-07-16
-- Purpose:
-- This stored procedure was written to create a folder.
-- It determines whether the fodler exists, and reports errors if it does
--
--
-- Example:
-- exec usp_create_folder 'd:\backups\backup20040716'
--
-- Possible future additions:
--
-- Change Control: version 1 - creation and adding of comment
--
declare @SQL varchar(500)
declare @ErrorType varchar(25)

create table #direxists
(FileExists int,
FileIsDir int,
ParentDirExists int)

insert into #direxists exec master..xp_fileexist @foldnm
if exists(select 'Dir Exists' from #direxists where fileExists = 0 and FileIsDir = 1 and ParentDirExists = 1)
begin
select @ErrorType 'Folder exists'
goto DirError
end
else
begin
select @sql = 'master..xp_cmdshell ''md '+ @foldnm +''''
exec (@sql)
insert into #direxists exec master..xp_fileexist @foldnm
if not exists(select 'Dir Exists' from #direxists where fileExists = 0 and FileIsDir = 1 and ParentDirExists = 1)
begin
select @ErrorType 'Folder Not Created'
goto DirError
end
print 'New Backup Directory '+@foldnm+' has been created'
end
goto finish

DirError:
Print '*** ERROR '+@ErrorType+' ***'
drop table #direxists
RETURN -1

Finish:
drop table #direxists
RETURN 0

END


I call it using the following code from a Job:

set nocount on

declare @new_backup_folder varchar(250)
declare @dateformat char(8)
declare @rc int

select @dateformat = convert(char(2),(datepart(day,getdate()) ))
+substring(convert(char(3),(datepart(month,getdate()) +100) ),2,2)
+convert(char(4),datepart(year,getdate()))
--debug display
--select @dateformat
--base folder
select @new_backup_folder = 'e:\Scrubbed_Backups\'+@dateformat
--debug display
--select @new_backup_folder
exec @rc=usp_create_folder @new_backup_folder
select @rc


So the return comes back with -1, BUT step completes succesfully.

Is there a way of forces step to fail? I cannot find anything in Ken's books, or BOL.

I could try and do everything in 1 step, I guess, but that means that restarting is quite as simple, if I resolve the underlying problem (for example permission to copy files, or a remote directory name, for step 3). I can't simply re-start step 3, but have to run code specifically to do only the remaining steps.

I could have a table, and insert a row into when an error occurs, and then check the table in every step, to avoid execution, and rely on my sent mail to inform me of the step where the failure ocurred.

Thoughts, Comment, General Laughter?

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!

mr_mist
Grunnio

1870 Posts

Posted - 2004-07-20 : 05:13:54
RAISEERROR?

-------
Moo. :)
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2004-07-20 : 05:30:42
*blushes*

how did I miss that !

sighs

Ta Moo

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page
   

- Advertisement -