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 2005 Forums
 Transact-SQL (2005)
 How to make job report 'failure'

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-10-27 : 06:47:40
I have a job with multiple steps. If one step succeeds or fails, i want it to go onto the next step. However, I want the job as a whole to report as failed if any steps fail(as opposed to the warning icon). How can I do this?

Hearty head pats

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-27 : 14:40:41
You can fail a job purposefully via RAISERROR.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-10-28 : 04:21:45
Hi Tara

Thanks for your reply. How would I do this? I assume I create an additional step that will execute the RAISERROR block, but I want to make sure that all steps execute prior to failing the job. For instance, I have 10 steps, steps 2 and 8 fail, but on failure, they go to the next step so that the rest all execute.

How do I know that 2 and 8 fail? Is there a way to store the results (@@ERROR) in global parameters that I can check in the final step?



Hearty head pats
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-10-28 : 14:02:24
Perhaps keep track of failure/success via a SQL table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

bmckd65
Starting Member

1 Post

Posted - 2009-12-16 : 18:40:16
Alternatively you could do something similar to following. If the count is greater than 0 then you have an error in one of the steps. Bear in mind that if you include the following SQL as a step in your job, and it fails once, then it will report a failure forever as it will detect a last_run_outcome of 0 for itself.

[use msdb]
if (
select count(*)
FROM [msdb].[dbo].[sysjobsteps]
where [last_run_outcome] = 0 and job_id = (
select job_id from dbo.sysjobs
where name = '<job_name>')
) > 0
BEGIN
RAISERROR('Failure detected in <job_name>.', 17, 1)
END
Go to Top of Page
   

- Advertisement -