Issue:

Unable to see a list of past job history. Only see the most recent.

Default settings limits to last 1000 records overall and last 100 records per job.

SELECT j.name, COUNT(*) Executions
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.sysjobhistory h
ON j.job_id = h.job_id
GROUP BY j.name
ORDER BY Executions DESC

Fix:

Make sure the Limit size of job history log is not enabled. Else you will not be able to see older jobs history.

SQL Agent -> Properties -> History

Note: If this is disabled, make sure you manually purge the sql job history.

DBA – Purge SQL Agent Job History

Executes Microsoft system stored procedures to remove all history from MSDB older than X days

use msdb
go
DECLARE @PurgeDate DATE
SET @PurgeDate = (select  CONVERT(date, (getdate()-30), 101) )
PRINT @PurgeDate
EXEC sp_purge_jobhistory   @oldest_date = @PurgeDate
USE msdb
GO
DECLARE @DaysToKeepHistory DATETIME
SET @DaysToKeepHistory = (SELECT CONVERT(VARCHAR(10), DATEADD(dd, -30, GETDATE()), 101))
EXEC sp_delete_backuphistory @DaysToKeepHistory
GO
USE msdb
GO

DECLARE @job nvarchar(128)
DECLARE @sql1 VARCHAR(max)
Declare @sql2 VARCHAR (MAX)
Declare @sql3 VARCHAR (MAX)
DECLARE @DaysToKeepHistory DATETIME

DECLARE jobnames CURSOR FOR 
		SELECT name from dbo.sysjobs (NOLOCK)
SET @DaysToKeepHistory =  (SELECT GETDATE()  - 30)
OPEN jobnames  
FETCH NEXT FROM jobnames INTO @job  

WHILE @@FETCH_STATUS = 0  
BEGIN  
       SET @sql1 = 'EXEC [dbo].[sp_delete_jobsteplog]      @job_name  = ' + CHAR (39) +  @job  + CHAR (39) 
	   SET @sql2  = ' ,  @older_than = ' + CHAR(39) +  CONVERT (CHAR (10), @DaysToKeepHistory, 101)  + CHAR(39) 
	   SET @sql3 = @sql1  +   @sql2 + ' ; ' 
       --PRINT @sql3
       EXEC (@sql3)
     
       FETCH NEXT FROM jobnames INTO @job  
END  

CLOSE jobnames  
DEALLOCATE jobnames

Sources:

https://blog.sqlauthority.com/2014/02/27/sql-server-dude-where-is-the-sql-agent-job-history-notes-from-the-field-017/

Last modified: March 1, 2022

Author

Comments

Write a Reply or Comment