Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

James Serra's Blog

James is currently a Senior Business Intelligence Architect/Developer and has over 20 years of IT experience. James started his career as a software developer, then became a DBA 12 years ago, and for the last five years he has been working extensively with Business Intelligence using the SQL Server BI stack (SSAS, SSRS, and SSIS). James has been at times a permanent employee, consultant, contractor, and owner of his own business. All these experiences along with continuous learning has helped James to develop many successful data warehouse and BI projects. James has earned the MCITP Business Developer 2008, MCITP Database Administrator 2008, and MCITP Database Developer 2008, and has a Bachelor of Science degree in Computer Engineering. His blog is at .

Easier way to view SQL Server job history

Ever get a timeout error when using the “View History” for a SQL Server Agent job? I find this happens when you have a lot of rows of job history and/or the SQL Server box is getting hit hard. So, I have a stored procedure that I use to display the job history using a couple of system tables that won’t time out on you. Plus the output of this stored procedure is easier to read than the “View History” dialog box. The procedure is below. It takes two parameters: Date, which will show you the job history for that date (and only that date), and Job Name. An example of how to execute it:

sp_ListJobRunHistory ’5/1/2011′, ‘Daily Feed Load’

Here is the stored procedure:

CREATE PROCEDURE [DBO].[Sp_listjobrunhistory] @dateparam DATETIME,
                                              @JobName   VARCHAR(100)
      SELECT sysjobhistory.server,
             CASE sysjobhistory.run_status
               WHEN 0 THEN 'Failed'
               WHEN 1 THEN 'Succeeded'
               ELSE '???'
             Isnull(Substring(CONVERT(VARCHAR(8), run_date), 1, 4) + '-' +
                                     (8), run_date), 5, 2) + '-' +
                              8), run_date), 7, 2), '')
             [Run DATE],
             Isnull(Substring(CONVERT(VARCHAR(7), run_time+1000000), 2, 2) + ':'
                           Substring(CONVERT(VARCHAR(7), run_time+1000000), 4, 2
                    ':' +
                    Substring(CONVERT(VARCHAR(7), run_time+1000000), 6, 2), '')
             [Run TIME],
             Isnull(Substring(CONVERT(VARCHAR(7), run_duration+1000000), 2, 2) +
                     ':' +
                           Substring(CONVERT(VARCHAR(7), run_duration+1000000),
                    + ':' +
                    Substring(CONVERT(VARCHAR(7), run_duration+1000000), 6, 2),
             ) AS
      FROM   msdb.dbo.sysjobhistory
             INNER JOIN msdb.dbo.sysjobs
               ON msdb.dbo.sysjobhistory.job_id = msdb.dbo.sysjobs.job_id
      WHERE  sysjobhistory.run_date = Datepart(yyyy, @dateparam) * 10000 +
                                             Datepart(mm, @dateparam) * 100 +
                                             dd, @dateparam)
             AND = @JobName --remove this line if you want to show all jobs for the specified day
      ORDER  BY instance_id DESC


No comments.

Leave a Comment

Please register or log in to leave a comment.