半个SQL查询按预期工作,需要在24小时内对故障进行额外查询

时间:2016-10-19 11:06:07

标签: sql sql-server

我得到了一些SQL查询编码的帮助,这给了我“工作活动”部分中失败工作显示的结果,但我需要帮助缩小生成的结果,只显示以名称'开头的工作维护 - 备份......'。这是我当前代码的link

例如。我会打开工作活动,然后从他的名字开始查看6个乔布斯。我会注意以下列'启用/状态/上次运行结果/上次运行/下次运行'然后我会进一步查看每个列的作业历史记录,并检查过去24小时是否有任何故障。

我还需要帮助更深入地查询每个工作历史,以便在24小时内为每个工作生成第二个所有工作失败的清单。

如果您需要有关我正在尝试报告/自动化的当前备份作业检查的更多信息或示例,请告诉我。

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT   SysJobs.name as 'Job Name'
    ,SysJobSteps.step_name as 'Step Name'
    ,Job.run_status as 'Run Status'
    ,Job.message as 'Error Message'
    ,Job.exec_date as 'Date Executed'
    ,CASE WHEN enabled=1 THEN 'Enabled'  
               ELSE 'Disabled'  
          END [Job Status]
    ,run_status
    ,exec_date
    ,((SUBSTRING(CAST(next_run_date AS VARCHAR(8)), 5, 2) + '/'
    + SUBSTRING(CAST(next_run_date AS VARCHAR(8)), 7, 2) + '/'
    + SUBSTRING(CAST(next_run_date AS VARCHAR(8)), 1, 4) + ' '
    + SUBSTRING((REPLICATE('0',6-LEN(CAST(next_run_time AS varchar)))
    + CAST(next_run_time AS VARCHAR)), 1, 2) + ':'
    + SUBSTRING((REPLICATE('0',6-LEN(CAST(next_run_time AS VARCHAR)))
    + CAST(next_run_time AS VARCHAR)), 3, 2) + ':'
    + SUBSTRING((REPLICATE('0',6-LEN(CAST(next_run_time as varchar)))
    + CAST(next_run_time AS VARCHAR)), 5, 2))) AS 'next_run_date' 
FROM    (SELECT Instance.instance_id
    ,DBSysJobHistory.job_id
    ,DBSysJobHistory.step_id
    ,DBSysJobHistory.sql_message_id
    ,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)
INNER JOIN msdb.dbo.sysjobschedules SysJobSchedule ON  SysJobs.job_id = SysJobSchedule.JOB_ID
WHERE  SysJobs.name like '%MAINTANANCE - BACKUP%'
AND Job.exec_date >= DATEADD(day, -1, GETDATE())