2014-02-06 59 views
2

我正在編寫一個查詢來查找當前正在SQL中運行的作業(我知道我們可以在Job Active Monitor中查看它,但我需要在TSQL中執行此操作)。儘管我可以查詢sysjobactivity表來查找當前正在運行的作業,但它並不能告訴正在運行的作業步驟(因爲我的作業可能有多於一個步驟)。SQL - 如何通過TSQL找到當前正在運行的作業步驟

查詢我所用:

SELECT s.name AS [JOB_NAME], 
     '' AS [STEP_ID], 
     '' AS STEP_NAME, 
     'Processing' AS STATUS, 
     sja.run_requested_date AS START_TIME, 
     null AS END_DATE, 
     convert(varchar, (getdate() - sja.run_requested_date), 8) AS Duration 
    FROM sysjobactivity sja, sysjobs s 
WHERE sja.job_id = s.job_id 
    AND sja.run_requested_date > getdate() - 1 
    AND sja.stop_execution_date IS NULL 

請幫我找出其中的工作目前工程進展步驟ID &步驟名稱。

+0

可能重複[如何確定工作狀態?](http://stackoverflow.com/questions/200195/how-can-i-determine-the-status-of-a-工作) –

回答

2

我想下面的腳本幫助來獲得SQL喬布斯與當前實施步驟,試試這個

msdb.dbo.sp_help_job @execution_status = 1 
+0

謝謝納文。我看到一列指定爲current_execution_step,我想它是我正在尋找的那一列。讓我深入瞭解並從哪個表或存儲過程中提取它。感謝您提供的信息。 – user3138788

+0

乾杯,非常好的答案 – levi

0
DECLARE @StepCount INT 
SELECT @StepCount = COUNT(1) 
FROM msdb.dbo.sysjobsteps 
WHERE job_id = '0523333-5C24-1526-8391-AA84749345666' --JobID 


SELECT 
     [JobName] 
     ,[JobStepID] 
     ,[JobStepName] 
     ,[JobStepStatus] 
     ,[RunDateTime] 
     ,[RunDuration] 
    FROM 
    (
     SELECT 
       j.[name] AS [JobName] 
      ,Jh.[step_id] AS [JobStepID] 
      ,jh.[step_name] AS [JobStepName] 
      ,CASE 
       WHEN jh.[run_status] = 0 THEN 'Failed' 
       WHEN jh.[run_status] = 1 THEN 'Succeeded' 
       WHEN jh.[run_status] = 2 THEN 'Retry (step only)' 
       WHEN jh.[run_status] = 3 THEN 'Canceled' 
       WHEN jh.[run_status] = 4 THEN 'In-progress message' 
       WHEN jh.[run_status] = 5 THEN 'Unknown' 
       ELSE 'N/A' 
       END AS [JobStepStatus] 
      ,msdb.dbo.agent_datetime(run_date, run_time) AS [RunDateTime] 
      ,CAST(jh.[run_duration]/10000 AS VARCHAR) + ':' + CAST(jh.[run_duration]/100%100 AS VARCHAR) + ':' + CAST(jh.[run_duration]%100 AS VARCHAR) AS [RunDuration] 
      ,ROW_NUMBER() OVER 
      (
       PARTITION BY jh.[run_date] 
       ORDER BY jh.[run_date] DESC, jh.[run_time] DESC 
      ) AS [RowNumber] 
     FROM 
      msdb.[dbo].[sysjobhistory] jh 
      INNER JOIN msdb.[dbo].[sysjobs] j 
       ON jh.[job_id] = j.[job_id] 
     WHERE 
      j.[name] = 'ProcessCubes' --Job Name 
      AND jh.[step_id] > 0 
      AND CAST(RTRIM(run_date) AS DATE) = CAST(GETDATE() AS DATE) --Current Date 
    ) A 
    WHERE 
     [RowNumber] <= @StepCount 
     AND [JobStepStatus] = 'Failed' 
+0

添加更多細節。 – Phiter

0

試試這個:

SELECT distinct 
     cast([sJOB].[job_id] as varchar(max)) AS execution_id 
    , [sJSTP].[step_name] AS executable_name 
    , [sJOB].[name] AS package_name 
    , CASE [sJSTP].[run_date] 
     WHEN 0 THEN NULL 
     ELSE 
      CAST(
       CAST([sJSTP].[run_date] AS CHAR(8)) 
       + ' ' 
       + STUFF(
        STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6) 
         , 3, 0, ':') 
        , 6, 0, ':') 
       AS DATETIME) 
     END AS start_time, 
     dateadd(ss, run_duration, CASE [sJSTP].[run_date] 
     WHEN 0 THEN NULL 
     ELSE 
      CAST(
       CAST([sJSTP].[run_date] AS CHAR(8)) 
       + ' ' 
       + STUFF(
        STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6) 
         , 3, 0, ':') 
        , 6, 0, ':') 
       AS DATETIME) 
     END) end_time 
-- , [sJSTP].[run_duration] [looptijd in minuten] 
    , CASE [sJSTP].[run_status] 
     WHEN 0 THEN 'Failed' 
     WHEN 1 THEN 'Success' 
     WHEN 2 THEN 'Retry' 
     WHEN 3 THEN 'Cancelled' 
     WHEN 5 THEN 'Unknown' 
     END AS execution_result_description 
FROM 
    [msdb].[dbo].[sysjobhistory] AS [sJSTP] 
    INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB] 
     ON [sJSTP].[job_id] = [sJOB].[job_id] 
    inner join [msdb].[dbo].[sysjobsteps] steps 
     ON [sJSTP].[job_id] = [steps].[job_id] 
where [sJSTP].[run_date] <> 0 
and CASE [sJSTP].[run_date] 
     WHEN 0 THEN NULL 
     ELSE 
      CAST(
       CAST([sJSTP].[run_date] AS CHAR(8)) 
       + ' ' 
       + STUFF(
        STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6) 
         , 3, 0, ':') 
        , 6, 0, ':') 
       AS DATETIME) 
     END between dateadd(hh, -20, getdate()) and getdate() 
and [sJSTP].[step_name] not in ('(Job outcome)') 
order by start_time desc 

此外,我用這個查詢,看看從步驟結果正在運行的SSIS工作。但是,這隻顯示完成的步驟,而不是正在運行的步驟。我仍然需要找到一個SQL來查看當前正在運行的步驟,並將其與本文合併。

select distinct 
    cast(e.execution_id as varchar(max)), 
    e.executable_name, 
    e.package_name, 
    CONVERT(datetime, es.start_time) AS start_time 
, CONVERT(datetime, es.end_time) AS end_time 
, datediff(mi, es.start_time, es.end_time) [running time] 
, case es.execution_result 
    when 0 then 'Success' 
    when 1 then 'Failed' 
    when 2 then 'Completion' 
    when 3 then 'Cancelled' 
    else cast(es.execution_result as varchar(max)) end as execution_result_description 
from ssisdb.catalog.executables e 
left join ssisdb.catalog.executable_statistics es 
on e.executable_id = es.executable_id 
and e.execution_id = es.execution_id 
order by 6 desc 
相關問題