Get date of last successful job run?

前端 未结 5 1427
萌比男神i
萌比男神i 2021-02-20 16:48

I have a single step job that executes a stored procedure. I would like get the date of the last successful job execution time so that I can just update a delta instead of the w

相关标签:
5条回答
  • 2021-02-20 16:49

    Using information from the following threads:

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=112427 http://www.sqlservercentral.com/Forums/Topic542581-145-1.aspx

    This is what I came up with...

    DECLARE 
        @statement nvarchar(72),
        @job_id uniqueidentifier,
        @last_run_date datetime
    
    SET @statement = 'SET @guid = CAST(' + SUBSTRING(APP_NAME(), 30, 34) + ' as uniqueidentifier)'
    
    EXECUTE sp_executesql @statement, N'@guid uniqueidentifier OUT', @guid = @job_id OUT
    
    SELECT TOP (1)
        @last_run_date = CAST(STR(run_date, 8, 0) as datetime) + CAST(STUFF(STUFF(STR(run_time, 6, 0), 3, 0, ':'), 6, 0, ':') as datetime)
    FROM msdb.dbo.sysjobhistory 
    WHERE job_id = @job_id
    AND run_status = 1
    ORDER BY
        CAST(STR(run_date, 8, 0) as datetime) + CAST(STUFF(STUFF(STR(run_time, 6, 0), 3, 0, ':'), 6, 0, ':') as datetime) DESC
    
    EXEC dbo.usp_UpdateFrom @last_run_date
    

    I'm not particularly comfortable with this, but I prefer this method of getting the job_id over depending on the job name.

    0 讨论(0)
  • 2021-02-20 16:57

    Since sysjobhistory only maintains a certain number of records, I recomend using sysjobactivity, which keeps the last execution "history" of each job and session.

    SELECT TOP 1 start_execution_date
    FROM msdb.dbo.sysjobactivity
    WHERE run_requested_date IS NOT NULL
    AND job_id = @job_id
    ORDER BY session_id DESC;
    

    NOTE: If a Job has not been executed during the life of a session, almost all values will be null.

    ALSO there is a system Stored Procedure sp_help_job that returns this information. It accepts job_id, enabled, etc. as parameters to return 1 or more records.

    0 讨论(0)
  • 2021-02-20 17:05

    To get Last successfully run jobs:

    SELECT 
        h.[job_id]
        ,j.Name JobName
        ,CONVERT(CHAR(10), CAST(STR(run_date,8, 0) AS dateTIME), 111)   [LastRunDate]
       ,STUFF(STUFF(RIGHT('000000' + 
        CAST (run_time AS`` VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') [LastRunTime]
       ,CASE run_status 
        WHEN 0 THEN 'Failed' 
        WHEN 1 THEN 'Succeeded' 
        WHEN 2 THEN 'Retry' 
        WHEN 3 THEN 'Cancelled' 
        WHEN 4 THEN 'In Progress' 
        END AS ExecutionStatus                                                 
        FROM [msdb].[dbo].[sysjobhistory] h 
        JOIN msdb.dbo.sysjobs j ON h.job_id=j.job_id 
        WHERE run_status=1 
        ORDER BY run_date DESC,run_time DESC
    
    0 讨论(0)
  • 2021-02-20 17:05

    The tables you want are sysjobs and sysjobhistory in msdb. Although be warned! SQL Server only maintains a certain number of records, so if there are too many jobs and the history is not large enough, you will end up with no history.

    The following code retrieves the job_id for the given job name, and queries the history table for the last successfully finished run (i.e. step 0, status 1). As you can see, you have to convert the run time back to a date, as SQL Server stores it in two int columns:

    DECLARE @job_id binary(16)
    SELECT @job_id = job_id FROM msdb.dbo.sysjobs WHERE (name = N'YourJobName')
    
    SELECT TOP 1
        CONVERT(DATETIME, RTRIM(run_date))
        + ((run_time / 10000 * 3600) 
        + ((run_time % 10000) / 100 * 60) 
        + (run_time % 10000) % 100) / (86399.9964) AS run_datetime
        , *
    FROM
        msdb..sysjobhistory sjh
    WHERE
        sjh.step_id = 0 
        AND sjh.run_status = 1 
        AND sjh.job_id = @job_id
    ORDER BY
        run_datetime DESC
    
    0 讨论(0)
  • 2021-02-20 17:11

    Have a look at this article, it may point you in the right direction. Unfortunately I don't have SQL Server on my home machine so can't test it out for you!

    You basically need to query the sysjobactivity table and get the values from start_execution_date and stop_execution_date. You'll need the job_id, but i'm not sure where you'll get that from.

    I hope this helps.

    EDIT Ok, I've done some more research and found the following code snippet

    DECLARE @jobId binary(16)
    
    SELECT @jobId = job_id FROM msdb.dbo.sysjobs WHERE (name = N'Name of Your Job')
    
    0 讨论(0)
提交回复
热议问题