SQL Server Agent job failed: find the real error in msdb job history
By Scott Coffey, Senior Database Developer. Updated 2026-10-23.
A nightly job fails and the first thing you see, in the failure email or the top line of the job history, is this:
The job failed. The Job was invoked by Schedule 12 (Nightly). The last step to run was step 2 (Load staging).
That line is the job outcome. It tells you which step stopped the job but not why. The actual error is recorded on a separate history row for the step, and sometimes it is not recorded in full anywhere unless you set the step up to keep it.
How Agent records history
Each run writes one row per step to msdb.dbo.sysjobhistory, plus one row with step_id = 0 for the job as a whole. The step rows carry the useful text in message, and for T-SQL steps the error number and severity in sql_message_id and sql_severity. run_status is 0 for failed, 1 succeeded, 2 retry, 3 canceled, and 4 in progress. A step that is set to retry writes a row with status 2 for each failed attempt before the final one, and those rows usually hold the same error.
In the SSMS Log File Viewer you have to expand the job row to see the step rows under it, which is why the generic outcome line is often all anyone reads.
Step 1: list recent failed steps with their messages
This query is read-only and needs SQLAgentReaderRole in msdb or sysadmin. It returns every failed or retried step in the last few days, newest first, with the error text.
DECLARE @Days int = 3;
SELECT j.name AS job_name,
h.step_id,
h.step_name,
msdb.dbo.agent_datetime(h.run_date, h.run_time) AS run_at,
CASE h.run_status WHEN 0 THEN 'Failed' WHEN 2 THEN 'Retry' END AS outcome,
h.sql_message_id,
h.sql_severity,
h.retries_attempted,
h.message
FROM msdb.dbo.sysjobhistory AS h
JOIN msdb.dbo.sysjobs AS j ON j.job_id = h.job_id
WHERE h.step_id > 0 -- step rows only, not the job outcome
AND h.run_status IN (0, 2) -- failed or retried
AND msdb.dbo.agent_datetime(h.run_date, h.run_time) >= DATEADD(DAY, -@Days, GETDATE())
ORDER BY h.instance_id DESC;
agent_datetime turns the integer run_date and run_time columns into a datetime. It ships in msdb but is undocumented, so if you prefer to avoid it, filter on run_date as an integer in yyyymmdd form instead.
Read the message column from the start. For a T-SQL step it begins with "Executed as user:" and then lists the errors with their message numbers, so a failed step might show Msg 2627 for a duplicate key or Msg 7399 for a linked server provider error. That number is what you search for, not the job outcome text.
Step 2: check who the step ran as
The "Executed as user:" part matters. A T-SQL step owned by a sysadmin runs as the Agent service account. A step owned by a non-sysadmin runs as the job owner, and PowerShell and CmdExec steps run as the Agent service account or a proxy. Many failures that work fine in SSMS come down to this: the step reads a file share, calls a linked server, or writes to a database that the service account or proxy cannot reach. Compare the account in the message with the one you tested as.
If the history shows "Unable to determine if the owner of job has server access", the job owner login no longer resolves, often because the person who created the job has left. Assign the job to a dedicated login with sp_update_job @owner_login_name.
Step 3: keep the full output
The history message has a length limit, and a step that prints a lot of output or a long PowerShell error can be cut off before the line that explains the failure. Send the step output somewhere that keeps all of it. An output file is the simplest option:
EXEC msdb.dbo.sp_update_jobstep
@job_name = N'DBA - Nightly load',
@step_id = 2,
@output_file_name = N'D:\SQLAgentLogs\Nightly_load_$(ESCAPE_SQUOTE(STEPID))_$(ESCAPE_SQUOTE(DATE)).txt',
@flags = 2; -- 2 = append to the output file
The tokens give one file per step per day, and the Agent service account needs write access to the folder. For T-SQL steps you can log to a table instead with @flags = 16 (append) and read it back with EXEC msdb.dbo.sp_help_jobsteplog @job_name = N'DBA - Nightly load';. Whichever you choose, clean up old files or log rows on a schedule.
Step 4: make PowerShell and CmdExec steps fail honestly
A step can also report success when the work failed, which hides the error entirely. In a PowerShell step, many cmdlet errors are non-terminating, so the script carries on and the step can succeed. Stop on the first error and throw a clear message:
$ErrorActionPreference = 'Stop'
try {
Invoke-Sqlcmd -ServerInstance 'SQL01' -Database 'Staging' `
-Query 'EXEC dbo.LoadDaily;' -QueryTimeout 0
}
catch {
throw "Load step failed: $($_.Exception.Message)"
}
For an Operating system (CmdExec) step, Agent decides success from the process exit code, which must match the step's @cmdexec_success_code (0 by default). A batch file or script that hits an error but still exits with 0 is recorded as a success, so return a non-zero exit code on failure.
Step 5: hear about the next one
Once the history is useful, make sure someone sees it. Set the job to email an operator on failure with sp_update_job @notify_level_email = 2, and keep the step 1 query handy so the email leads straight to the step error. The SQL Rig pack includes a job inventory script that lists every job with its last outcome, owner warnings, and failure count, and a failure alert script that creates the operator and notifications with a dry run first.
Check the fix
Run the job manually with EXEC msdb.dbo.sp_start_job @job_name = N'DBA - Nightly load';, then rerun the step 1 query. You should see the specific error number and text for the failing step, and the output file or step log should hold the complete output. Fix the underlying error, run the job once more, and confirm the step rows show status 1.