Wednesday, March 13, 2019

View Job History using tSQL, SQL Server

We can view job history using SSMS very easily. But when the amount of log is too high in count, SSMS got timeout exception to view the job history.

For what, I always use below SQL to check the job history, which is almost like to GUI view :)

NOTE: you can comment the WHERE condition to see the full history of all jobs!

t-SQL
------------
SELECT top 100
b.instance_id,
  msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime',
b.step_id,b.step_name,
b.message,
 ((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60)
          as 'RunDurationMinutes'
FROM msdb.dbo.sysjobhistory  b
INNER JOIN msdb.dbo.sysjobs a
ON a.job_id = b.job_id 
WHERE a.name = 'your job name'
ORDER BY b.instance_id DESC

No comments:

Post a Comment