创建一个 SQL 查询以列出 24 小时内的作业失败
Creating a SQL Query to list Job failures within a 24hr period
我需要帮助创建针对 'Job Activies' 的 SQL 查询,用于 diff SQL 服务器上大约 5/10 的作业。基本上我需要生成如下列表,让我每次都进入工作 Activity 显示以下工作,理想情况下任何以名称 'MAINTENANCE - BACKUP..' 开头,然后是以下列 - Enabled/Status/Last 运行 Outcome/Last Run/Next 运行
一旦我可以确定哪些作业已启用、已成功等。然后我会在 24 小时内检查每个作业的历史记录,记下每个失败的时间和 Error/Reason 原因。我在 Internet 上有多个查询,这些查询提供了我正在寻找的详细信息,但我不确定如何重新订购 code/query 并直接挑选出我需要的内容。
您可以从这段代码创建一个视图,然后 select 通过日期从中创建视图:
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)
这是您需要的最近 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)
我需要帮助创建针对 'Job Activies' 的 SQL 查询,用于 diff SQL 服务器上大约 5/10 的作业。基本上我需要生成如下列表,让我每次都进入工作 Activity 显示以下工作,理想情况下任何以名称 'MAINTENANCE - BACKUP..' 开头,然后是以下列 - Enabled/Status/Last 运行 Outcome/Last Run/Next 运行
一旦我可以确定哪些作业已启用、已成功等。然后我会在 24 小时内检查每个作业的历史记录,记下每个失败的时间和 Error/Reason 原因。我在 Internet 上有多个查询,这些查询提供了我正在寻找的详细信息,但我不确定如何重新订购 code/query 并直接挑选出我需要的内容。
您可以从这段代码创建一个视图,然后 select 通过日期从中创建视图:
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)
这是您需要的最近 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)