哪里可以找到日期&当前运行SQL代理作业STEP的时间?

时间:2016-09-14 11:15:05

标签: sql-server sql-server-2012 sql-agent-job

我想查询当前正在运行的SQL代理作业步骤的视图或表,以及它们何时启动。

我在下面尝试过查询但是它给了我JOB日期时间而不是步骤日期时间。

select top 100 * from msdb.dbo.sysjobsteps
select top 100 * from msdb.dbo.sysjobstepslogs
select top 100 * from msdb.dbo.sysjobhistory 
exec master.dbo.xp_sqlagent_enum_jobs 1  , garbage -- gives me currently running job step

从SQL活动监视器下面显示所需值的屏幕截图。

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试以下

SELECT
    ja.job_id as JobId,
    j.name AS JobName,
    ja.start_execution_date as StartDn,      
    ISNULL(last_executed_step_id,0)+1 AS CurrentStepId
FROM msdb.dbo.sysjobactivity ja 
LEFT JOIN msdb.dbo.sysjobhistory jh ON ja.job_history_id = jh.instance_id
INNER JOIN msdb.dbo.sysjobs j ON ja.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobsteps js ON ja.job_id = js.job_id AND ISNULL(ja.last_executed_step_id,0)+1 = js.step_id
INNER JOIN msdb.dbo.syssessions r ON r.session_id = ja.session_id
WHERE start_execution_date is not null
AND stop_execution_date is null;

您可以查看此article以获取更多信息

希望这会对你有所帮助

答案 1 :(得分:0)

/* Hank Freeman */
use master
go
SELECT
    ja.job_id,
    j.name AS job_name,
    ja.start_execution_date, 
    --2019-06-10 18:54:31.000
    getdate() as 'NOW',
    '0'+Cast(Datepart(hh,(getdate() -  ja.start_execution_date)) as char(2)) + ':' +
    Cast(Datepart(n,(getdate() -  ja.start_execution_date))  as char(2)) + ':'  +
    '0'+Cast(Datepart(ss,(getdate() -  ja.start_execution_date)) as char(2))    as 'Duration',
    Cast(DATEDIFF(n,ja.start_execution_date,getdate())  as char(5))     as 'Duration_Mins',
    ISNULL(last_executed_step_id,0)+1 AS cur_exec_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 session_id DESC
    )
AND start_execution_date is not null
AND stop_execution_date is null
order by 2
;