dataiotech Labs
Scripts Library/sqlserver/#53
Script #53
sqlserver
Monitoringread-onlyMin Version: 2008

SQL Server Agent Job Last-Run Status

Lists failed jobs and their latest run results. Catches backup and maintenance jobs failing silently.

#sql-agent#jobs#msdb
sqlserver · sql
SELECT j.name AS job_name, j.enabled,
       CASE h.run_status WHEN 0 THEN 'Failed' WHEN 1 THEN 'Succeeded' WHEN 2 THEN 'Retry'
                         WHEN 3 THEN 'Canceled' ELSE 'Unknown' END AS last_status,
       msdb.dbo.agent_datetime(h.run_date, h.run_time) AS last_run,
       h.message
FROM msdb.dbo.sysjobs j
OUTER APPLY (SELECT TOP 1 * FROM msdb.dbo.sysjobhistory
             WHERE job_id = j.job_id AND step_id = 0 ORDER BY instance_id DESC) h
ORDER BY CASE WHEN h.run_status = 0 THEN 0 ELSE 1 END, last_run DESC;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 2008).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.

Engineering Triad: Related Runbooks & Incident Response

Which production incidents and emergency drills execute this script as a diagnosis step?

Runbook Library (63)
Related Alerts & Metrics
M33SQL Server Last Backup AgeSQL Server