查看具有作业开始时间的SQL Server中正在运行的作业的脚本


16

如何编写脚本以查看具有作业开始时间的SQL Server中正在运行的作业?

SELECT sj.name, 
 sja.run_requested_date, 
 CONVERT(VARCHAR(12), sja.stop_execution_date-sja.start_execution_date, 114) Duration
FROM msdb.dbo.sysjobactivity sja
INNER JOIN msdb.dbo.sysjobs sj
ON sja.job_id = sj.job_id
WHERE sja.run_requested_date IS NOT NULL
ORDER BY sja.run_requested_date desc;

不知道我明白这个问题。您是否只想查看当前正在运行的作业?
亚伦·伯特兰

Answers:


49

我不久前发布了一个查询,以获取此处当前正在运行的作业的列表。

SELECT
    ja.job_id,
    j.name AS job_name,
    ja.start_execution_date,      
    ISNULL(last_executed_step_id,0)+1 AS current_executed_step_id,
    Js.step_name
FROM msdb.dbo.sysjobactivity ja 
LEFT JOIN msdb.dbo.sysjobhistory jh 
    ON ja.job_history_id = jh.instance_id
JOIN msdb.dbo.sysjobs j 
ON ja.job_id = j.job_id
JOIN msdb.dbo.sysjobsteps js
    ON ja.job_id = js.job_id
    AND ISNULL(ja.last_executed_step_id,0)+1 = js.step_id
WHERE ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)
AND start_execution_date is not null
AND stop_execution_date is null;

它具有工作名称,工作ID,开始时间以及正在执行的步骤。如果您点击上面的链接,则可以获得有关我如何提出它以及其他可用内容的更多详细信息。


6
不幸的是,该脚本假定当前正在运行的步骤是最后完成的步骤之后的步骤。并非总是如此。
Todd McDermid

3

我遇到了我的一台服务器查询MSDB表(上面列出的代码)的问题,因为我的一项工作即将开始运行,但事实并非如此。有一个系统存储过程可以返回执行状态,但是没有错误就不能执行插入exec语句。在里面是可以与insert exec语句一起使用的另一个系统存储过程。

INSERT INTO #Job
EXEC master.dbo.xp_sqlagent_enum_jobs 1,dbo

然后将该表加载到:

CREATE TABLE #Job 
           (job_id               UNIQUEIDENTIFIER NOT NULL,  
           last_run_date         INT              NOT NULL,  
           last_run_time         INT              NOT NULL,  
           next_run_date         INT              NOT NULL,  
           next_run_time         INT              NOT NULL,  
           next_run_schedule_id  INT              NOT NULL,  
           requested_to_run      INT              NOT NULL, -- BOOL  
           request_source        INT              NOT NULL,  
           request_source_id     sysname          COLLATE database_default NULL,  
           running               INT              NOT NULL, -- BOOL  
           current_step          INT              NOT NULL,  
           current_retry_attempt INT              NOT NULL,  
           job_state             INT              NOT NULL) 

1

这将为您提供作业名称以及运行日期和时间,如果您只想查看其运行的名称和时间,则可以删除日期。

SELECT
j.name AS 'JobName',
run_date,
run_time,
msdb.dbo.agent_datetime(run_date, run_time) AS 'RunDateTime'
FROM msdb.dbo.sysjobs j 
JOIN msdb.dbo.sysjobhistory h ON j.job_id = h.job_id 
WHERE j.enabled = 1  --Only Enabled Jobs
ORDER BY JobName, RunDateTime desc

+1 for msdb.dbo.agent_datetime(run_date,run_time)
Alocyte

1

该脚本将显示当前正在运行的作业以及当前正在执行的步骤:

-- From http://www.databasejournal.com/features/mssql/article.php/10894_3491201_2/Detecting-The-State-of-a-SQL-Server-Agent-Job.htm
    CREATE TABLE #ENUM_JOB 
    ( 
        Job_ID UNIQUEIDENTIFIER, 
        Last_Run_Date INT, 
        Last_Run_Time INT, 
        Next_Run_Date INT, 
        Next_Run_Time INT, 
        Next_Run_Schedule_ID INT, 
        Requested_To_Run INT, 
        Request_Source INT, 
        Request_Source_ID VARCHAR(100), 
        Running INT, 
        Current_Step INT, 
        Current_Retry_Attempt INT, 
        State INT 
    )
    INSERT INTO #ENUM_JOB 
         EXEC master.dbo.xp_sqlagent_enum_jobs 1,garbage
    SELECT E.*, SJ.name AS job_name, SJS.step_name
        FROM #ENUM_JOB AS E
        JOIN msdb.dbo.sysjobs AS SJ
            ON SJ.job_id = E.Job_ID
        JOIN msdb.dbo.sysjobsteps AS SJS
            ON SJS.job_id = SJ.job_id
            AND SJS.step_id = E.Current_Step
    DROP TABLE #ENUM_JOB

0

以下脚本返回:

  • 工作名称
  • 工作编号
  • 工作要求的时间
  • 作业执行时间
SELECT sj.name
   , sja.*
FROM msdb.dbo.sysjobactivity AS sja
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id
WHERE sja.start_execution_date IS NOT NULL
   AND sja.stop_execution_date IS NULL

3
对我来说,此查询返回的是实际上未运行的禁用作业
Nick.McDermaid 2015年

1
@Kenneth的答案更好,因为它仅返回当前正在运行的作业。
尼拉吉

0

可能在房屋周围一些,但能解决问题

;WITH cJobDetail AS (
    SELECT 
        ja.job_id
        ,j.name
        ,ja.start_execution_date
        ,ja.last_executed_step_id
        --,j.start_step_id
        ,CASE 
            WHEN j.start_step_id = COALESCE(ja.last_executed_step_id,j.start_step_id) THEN j.start_step_id
            ELSE NULL
            END AS ExecutingStepId
        ,ja.job_history_id
        ,DATEDIFF(HOUR, ja.start_execution_date, COALESCE(ja.stop_execution_date,SYSDATETIME())) AS JobHourRunning
        ,DATEDIFF(SECOND, ja.start_execution_date, COALESCE(ja.stop_execution_date,SYSDATETIME())) / 60 % 60 AS JobMinuteRunning
        ,DATEDIFF(SECOND, ja.start_execution_date, COALESCE(ja.stop_execution_date,SYSDATETIME())) % 60 AS JobSecondRunning 
    FROM dbo.sysjobactivity ja
    INNER JOIN dbo.sysjobs j
        ON ja.job_id = j.job_id
    WHERE ja.start_execution_date IS NOT NULL
    AND ja.stop_execution_date IS NULL
)
,MaxJobHistoryStepStatus AS (
    SELECT jh.job_id, jh.step_id, MAX(jh.instance_id) AS MaxInstanceId
    FROM cJobDetail jd
    INNER JOIN dbo.sysjobhistory jh
        ON jd.job_id = jh.job_id
        AND jd.last_executed_step_id = jh.step_id
    GROUP BY jh.job_id, jh.step_id
    )

SELECT 
    jd.job_id
    ,jd.name
    ,jd.start_execution_date 
    ,COALESCE(
                CASE jh.run_status 
                        WHEN 1 THEN 
                                    CASE js.on_success_action
                                                            WHEN 3 THEN jd.last_executed_step_id + 1
                                                            WHEN 4 THEN js.on_success_step_id
                                    END
                        WHEN 0 THEN 
                                    CASE js.on_fail_action
                                                        WHEN 3 THEN jd.last_executed_step_id + 1
                                                        WHEN 4 THEN js.on_fail_step_id
                                    END
                END,jd.ExecutingStepId) AS StepId
    ,jd.JobHourRunning
    ,jd.JobMinuteRunning
    ,jd.JobSecondRunning                                
FROM cJobDetail jd
LEFT JOIN MaxJobHistoryStepStatus mhs
    ON jd.job_id = mhs.job_id
LEFT JOIN dbo.sysjobhistory jh
    ON mhs.MaxInstanceId = jh.instance_id
LEFT JOIN dbo.sysjobsteps js
    ON mhs.job_id = js.job_id
    AND mhs.step_id = js.step_id
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.