view log to table job latest job

Marco Bakker | DBA.nl 72 Reputation points
2021-08-04T17:28:13.85+00:00

Dear SQL friends,

I would like to query the history of a job. I know you can see via log to table this info, but this is only the latest job.
Is there a way to view the same info of jobs run before the latest one?

kind regards,
Marco

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,705 questions
0 comments No comments
{count} votes

Accepted answer
  1. Guoxiong 8,206 Reputation points
    2021-08-04T18:47:02.857+00:00

    Not sure what you really want. Here is an example script to list the job execution history for the job "syspolicy_purge_history":

    USE [msdb];
    GO
    
    SELECT j.*, jh.*
      FROM [dbo].[sysjobs] AS j
    INNER JOIN [dbo].[sysjobhistory] AS jh 
        ON j.job_id = jh.job_id
     WHERE j.name = 'syspolicy_purge_history'   -- Job Name
       AND jh.step_id = 0                       -- Job level
    ORDER BY jh.run_date DESC;
    GO
    

1 additional answer

Sort by: Most helpful
  1. Seeya Xi-MSFT 16,576 Reputation points
    2021-08-05T04:04:30.323+00:00

    Hi @Anonymous ,

    Please refer to this docs: View the Job History
    Simply you can use the first two methods: SSMS or T-SQL.
    SSMS:
    120701-1.png

    T-SQL (for example):

    USE msdb ;    
    GO    
      
    EXEC dbo.sp_help_jobhistory     
        @job_name = N'syspolicy_purge_history' ;    
    GO    
    

    Best regards,
    Seeya


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.