SQL Server – Query to View Failure Detail for SQL Agent Job Step

sql serversql server 2014

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 :

  1. I know the job name.
  2. I know when the job failed.
  3. I know the step number

Best Answer

  1. You can get failure reason by looking at message section of SQL-Agent Job history window.

  2. 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)
  1. If the error produce more than 4000 characters, enable following setting from particular Job -> Step Properties (you can click View button for complete error message):

enter image description here