How can I view full SQL Job History?
Asked Answered
R

5

30

In SQL Server Management Studio, when I "View History" for a SQL Job, I'm only shown the last 50 executions of the Job.

How can I view a full log of every execution of a SQL Job since it was created on the server?

Rattlepate answered 13/10, 2011 at 12:49 Comment(0)
T
44

The SQL Server Job system limits the total number of job history entries both per job and over the whole system. This information is stored in the MSDB database.

Obviously you won't be able to go back and see information that has been since discarded, but you can change the SQL Server Agent properties and increase the number of entries that will be recorded from now on.

In the SQL Server Agent Properties:

  • Select the History page
  • Modify the 'Maximum job history log size (rows)' and 'Maximum job history rows per job' to suit, or change how historical job data is deleted based on its age.

It won't give you back your history, but it'll help with your future queries!

Thunderstone answered 13/10, 2011 at 12:53 Comment(0)
T
4

I'm pretty sure job history is stored somewhere in a dedicated database in SQL Server itself. If this is the case, you can use SQL Server Profiler to intercept SQL statements sent by SQL Server Management Studio and find out names of tables, etc.

Tocharian answered 13/10, 2011 at 12:52 Comment(1)
+1 This is a generally useful technique whenever you know how to do something via the UI but aren't sure where SSMS gets the info from.Defence
U
3

Your outcome depends on a couple of things.

  1. What you've set your "Limit size of job log history" and "Automatically remove agent history" settings [right click on SQL Agent | Properties | History] and
  2. whether or not you are doing a "History Clean Up" task in a Maintenance Plan (or manually for that manner). The MP task runs the msdb.dbo.sp_purge_jobhistory stored procedure with an "oldest date" parameter which equates to the period you have selected.
Undersea answered 7/9, 2012 at 22:59 Comment(0)
O
0

You could use Temporal Table to change retention of data. Persisting job history in Azure SQL Managed Instance:

ALTER TABLE [msdb].[dbo].[sysjobhistory]
ADD StartTime DATETIME2 NOT NULL DEFAULT ('19000101 00:00:00.0000000')

ALTER TABLE [msdb].[dbo].[sysjobhistory]
ADD EndTime DATETIME2 NOT NULL DEFAULT ('99991231 23:59:59.9999999')

ALTER TABLE [msdb].[dbo].[sysjobhistory]
ADD PERIOD FOR SYSTEM_TIME (StartTime, EndTime)

ALTER TABLE [msdb].[dbo].[sysjobhistory]
ADD CONSTRAINT PK_sysjobhistory PRIMARY KEY (instance_id, job_id, step_id)

ALTER TABLE [msdb].[dbo].[sysjobhistory]
SET(SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[sysjobhistoryall],
    DATA_CONSISTENCY_CHECK = ON, HISTORY_RETENTION_PERIOD = 1 MONTH))

select * from msdb.dbo.sysjobhistoryall

This approach allows to define retention period as time(here 1 MONTH) instead of maximum number of rows per job/xaximum job history log size (rows).

Overtask answered 20/9, 2018 at 18:1 Comment(0)
C
0

To view the job history log In Object Explorer, connect to an instance of Database Engine.

On the Standard bar, click New Query.

Copy and paste the following example into the query window and click Execute.


      -- lists all job information for the NightlyBackups job. 
USE msdb ;  
GO  
EXEC dbo.sp_help_jobhistory   
    @job_name = 'Job_name' ;  
GO  

There are multiple parameters as per your needs For more information

https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-help-jobhistory-transact-sql?view=sql-server-ver16

Classmate answered 13/6 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.