I have a sql agent job step that failed for a given date / time. Is there an MSDB - TSQL query I can run to view the step failure reason given that :
- I know the job name.
- I know when the job failed.
- I know the step number
I have a sql agent job step that failed for a given date / time. Is there an MSDB - TSQL query I can run to view the step failure reason given that :
You should be able to adjust this query to your criteria - taken from What is the Query to display the failed sql jobs
select j.name
,js.step_name
,jh.sql_severity
,jh.message
,jh.run_date
,jh.run_time
FROM msdb.dbo.sysjobs AS j
INNER JOIN msdb.dbo.sysjobsteps AS js
ON js.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobhistory AS jh
ON jh.job_id = j.job_id AND jh.step_id = js.step_id
WHERE jh.run_status = 0
You can get failure reason by looking at message section of SQL-Agent Job history window.
If the error produce more than 1024 characters, you can query msdb.dbo.sysjobhistory, to be specific to the requirement, you can use following query:
Declare @job_name sysname = 'YourJobName',
@DateYYYYMMDD int = 20201030,
@step_num int = 1;
select message
from msdb.dbo.sysjobhistory as jh
where run_status = 0
and step_id = @step_num
and run_date = @DateYYYYMMDD
and exists (select 1 from msdb.dbo.sysjobs where name = @job_name and job_id = jh.job_id)