Wednesday, February 27, 2013

Script to check the last run status of job

SQL script to check the last run status of a job. This code will retrieve the last run status.


ECLARE @last_run_outcome INT 
SET @last_run_outcome = ( 
SELECT MAX(sjs.last_run_outcome) 
FROM msdb.dbo.sysjobs_view sjv 
INNER JOIN msdb.dbo.sysjobsteps sjs ON sjs.job_id = sjv.job_id 
WHERE name in ('MDR20') 
AND last_run_outcome = 0) 

--SELECT @last_run_outcome  

IF @last_run_outcome = 0 
EXEC msdb.dbo.sp_stop_job N'MDR20' 

Below is the outcome of the job for the last time ran:

0 = Failed
1 = Succeeded
3 = Canceled
5 = Unknown

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.