Sometimes you need to get to know the status of particular SQL Server Agent job on a given date. Below Stored Procedure can be used to check whether supplied SQL Agent Job has completed successfully on the given date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
CREATE PROCEDURE [dbo].[usp_Check_SQLAgentJob_Run] (@jobname AS NVARCHAR(MAX), @date DATETIME) AS DECLARE @job_rundate NVARCHAR(20) SET @job_rundate = CAST(YEAR(@date) AS VARCHAR(4)) + RIGHT('0'+ CAST(MONTH(@date) AS VARCHAR(2)), 2) + RIGHT('0'+ CAST(DAY(@date) AS VARCHAR(2)), 2) IF @job_rundate IN ( SELECT h.run_date FROM msdb.dbo.sysjobhistory AS h INNER JOIN msdb.dbo.sysjobs AS j ON h.job_id = j.job_id WHERE j.name = @jobname AND h.step_id = 0 AND h.run_status = 1 ) BEGIN Print '"' +@jobname +'" SQL Agent job has completed Successfully on : ' + CAST(@job_rundate as NVARCHAR(20)) END ELSE BEGIN Print '"' +@jobname +'" SQL Agent job has not completed Successfully on : ' + CAST(@job_rundate as NVARCHAR(20)) END |
Leave a Comment