Skip to content

Instantly share code, notes, and snippets.

@sqlpadwan
Last active March 11, 2018 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sqlpadwan/493978802f05b4dec46808cde3de84c8 to your computer and use it in GitHub Desktop.
Save sqlpadwan/493978802f05b4dec46808cde3de84c8 to your computer and use it in GitHub Desktop.
Getting the last 5 days job history
SELECT
name,
steps.step_name,
command,
steps.server,
database_name,
hist.run_date,
hist.run_time,
hist.run_duration,
hist.run_status,
hist.message,
hist.sql_severity
FROM msdb.dbo.sysjobs job
INNER JOIN msdb.dbo.sysjobsteps steps ON job.job_id = steps.job_id
INNER JOIN msdb.dbo.sysjobhistory hist ON hist.job_id = steps.job_id and hist.step_id = steps.step_id
WHERE job.enabled = 1
and hist.run_date > CONVERT(char(8), DATEADD(day, -5, GETDATE()), 112)
and run_status = 0
order by hist.run_date desc, hist.run_time
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment