我需要帮助创建一个针对' Job Activies' for diff SQL Server上的5/10作业。基本上我需要生成一个如下所示的列表,每次显示以下工作时,我都会进入工作活动,任何以名称' MAINTENANCE - BACKUP ...'开头。理想情况下,然后是以下列 - 启用/状态/上次运行结果/上次运行/下次运行
一旦我可以确定哪些工作已启用,请成功等。然后,我会在24小时内查看每个工作的历史记录,记下每次失败的时间和错误/原因。我在互联网上有多个查询,它给我提供了我正在寻找的详细信息,但我不确定如何重新订购代码/查询,只是挑选出我需要的具体内容。
答案 0 :(得分:1)
您可以从此代码创建一个视图,并通过日期选择它:
SELECT Job.instance_id
,SysJobs.job_id
,SysJobs.name as 'JOB_NAME'
,SysJobSteps.step_name as 'STEP_NAME'
,Job.run_status
,Job.sql_message_id
,Job.sql_severity
,Job.message
,Job.exec_date
,Job.run_duration
,Job.server
,SysJobSteps.output_file_name
FROM (SELECT Instance.instance_id
,DBSysJobHistory.job_id
,DBSysJobHistory.step_id
,DBSysJobHistory.sql_message_id
,DBSysJobHistory.sql_severity
,DBSysJobHistory.message
,(CASE DBSysJobHistory.run_status
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
END) as run_status
,((SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 5, 2) + '/'
+ SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 7, 2) + '/'
+ SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 1, 4) + ' '
+ SUBSTRING((REPLICATE('0',6-LEN(CAST(DBSysJobHistory.run_time AS varchar)))
+ CAST(DBSysJobHistory.run_time AS VARCHAR)), 1, 2) + ':'
+ SUBSTRING((REPLICATE('0',6-LEN(CAST(DBSysJobHistory.run_time AS VARCHAR)))
+ CAST(DBSysJobHistory.run_time AS VARCHAR)), 3, 2) + ':'
+ SUBSTRING((REPLICATE('0',6-LEN(CAST(DBSysJobHistory.run_time as varchar)))
+ CAST(DBSysJobHistory.run_time AS VARCHAR)), 5, 2))) AS 'exec_date'
,DBSysJobHistory.run_duration
,DBSysJobHistory.retries_attempted
,DBSysJobHistory.server
FROM msdb.dbo.sysjobhistory DBSysJobHistory
JOIN (SELECT DBSysJobHistory.job_id
,DBSysJobHistory.step_id
,MAX(DBSysJobHistory.instance_id) as instance_id
FROM msdb.dbo.sysjobhistory DBSysJobHistory
GROUP BY DBSysJobHistory.job_id
,DBSysJobHistory.step_id
) AS Instance ON DBSysJobHistory.instance_id = Instance.instance_id
WHERE DBSysJobHistory.run_status <> 1
) AS Job
JOIN msdb.dbo.sysjobs SysJobs
ON (Job.job_id = SysJobs.job_id)
JOIN msdb.dbo.sysjobsteps SysJobSteps
ON (Job.job_id = SysJobSteps.job_id AND Job.step_id = SysJobSteps.step_id)
答案 1 :(得分:0)
这是您需要的最后24小时工作详情...或者它无论如何都会让您开始。
select
j.name as JobName,
j.enabled,
j.date_created,
a.start_execution_date,
a.stop_execution_date,
a.next_scheduled_run_date,
h.run_date,
h.run_time,
case
when h.run_status = 0 then 'Failed'
when h.run_status = 1 then 'Succeeded'
when h.run_status = 2 then 'Retry'
when h.run_status = 3 then 'Canceled'
end as LastRunOutcome
from
msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobactivity a on
a.job_id = j.job_id
inner join msdb.dbo.sysjobhistory h on
h.job_id = j.job_id
inner join
(select job_id, max(run_date) dt
from msdb.dbo.sysjobhistory
group by job_id) h2 on h2.job_id = h.job_id and h2.dt = h.run_date
where
cast(a.start_execution_date as date) = cast(getdate() as date)