|
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 directorystep (2) : copy files to new directorystep (3) : delete an old direcorystep (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_foldergoset nocount ongocreate procedure usp_create_folder @foldnm varchar(250)asBEGIN-- 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 @foldnmif exists(select 'Dir Exists' from #direxists where fileExists = 0 and FileIsDir = 1 and ParentDirExists = 1) begin select @ErrorType 'Folder exists' goto DirErrorendelse 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'endgoto finish DirError:Print '*** ERROR '+@ErrorType+' ***'drop table #direxistsRETURN -1Finish:drop table #direxistsRETURN 0END I call it using the following code from a Job:set nocount ondeclare @new_backup_folder varchar(250)declare @dateformat char(8)declare @rc intselect @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 folderselect @new_backup_folder = 'e:\Scrubbed_Backups\'+@dateformat--debug display--select @new_backup_folderexec @rc=usp_create_folder @new_backup_folderselect @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! |
|