how to know status of currently running jobs

前端 未结 9 1330
难免孤独
难免孤独 2020-12-24 04:43

I need to know if a given Job is currently running on Ms SQL 2008 server. So as to not to invoke same job again that may lead to concurrency issues.

9条回答
  •  猫巷女王i
    2020-12-24 05:34

    I found a better answer by Kenneth Fisher. The following query returns only currently running jobs:

    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;
    

    You can get more information about a job by adding more columns from msdb.dbo.sysjobactivity table in select clause.

提交回复
热议问题