想要列出在SQL Server中手动执行的所有作业

时间:2019-04-08 15:11:48

标签: sql-server sql-job

如何获取上周手动执行的所有作业的列表?

2 个答案:

答案 0 :(得分:0)

您应该能够从msdb.dbo.sysjobhistory表中获取此信息。 访问this link以获得更多相关信息。

这里是一个样本。希望对您有帮助,我的朋友。

SELECT sj.name,
       sjh.step_name,
       sjh.step_id,
       sjh.run_status,
       STUFF(STUFF(CAST(sjh.run_date as nvarchar(10)),5,0,'.'),8,0,'.') as HistRunDate,
       STUFF(STUFF(RIGHT(REPLICATE('0', 6) +  CAST(sjh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') as HistRunTime,
       STUFF(STUFF(STUFF(RIGHT(REPLICATE('0', 8) + CAST(sjh.run_duration as varchar(8)), 8), 3, 0, ':'), 6, 0, ':'), 9, 0, ':') as HistRunDuration
       ,sjh.run_status AS JobStatus
FROM   msdb.dbo.sysjobs           AS sj
       join msdb.dbo.sysjobhistory AS sjh 
            on sjh.job_id = sj.job_id
WHERE  1=1

AND ((sjh.run_date = 20190401 and sjh.run_time > 200000) 
or (sjh.run_date = 20190409 and sjh.run_time < 100000))

ORDER BY
       sjh.run_date,
       sjh.run_time

答案 1 :(得分:0)

OP询问如何告诉所有手动执行的作业。为此,Tomato的查询应包括引用“ run_requested_source”列的msdb.dbo.sysjobactivity联接。

来自Microsoft。

run_requested_source(谁要求运行该作业)

1 = SOURCE_SCHEDULER
2 = SOURCE_ALERTER
3 = SOURCE_BOOT
4 = SOURCE_USER
6 = SOURCE_ON_IDLE_SCHEDULE

    SELECT sj.name,
       sjh.step_name,
       sjh.step_id,
       sjh.run_status,
       STUFF(STUFF(CAST(sjh.run_date as nvarchar(10)),5,0,'.'),8,0,'.') as HistRunDate,
       STUFF(STUFF(RIGHT(REPLICATE('0', 6) +  CAST(sjh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') as HistRunTime,
       STUFF(STUFF(STUFF(RIGHT(REPLICATE('0', 8) + CAST(sjh.run_duration as varchar(8)), 8), 3, 0, ':'), 6, 0, ':'), 9, 0, ':') as HistRunDuration
       ,sjh.run_status AS JobStatus

FROM   msdb.dbo.sysjobs           AS sj
       join msdb.dbo.sysjobhistory AS sjh on sjh.job_id = sj.job_id
       join msdb.dbo.sysjobactivity AS sa on sj.job_id = sa.job_id
WHERE
    (
        ((sjh.run_date = 20190401 and sjh.run_time > 200000) 
        or (sjh.run_date = 20190409 and sjh.run_time < 100000))
    )
    and sa.run_requested_source = 4

ORDER BY
       sjh.run_date,
       sjh.run_time