SQL Server中的作业历史记录

时间:2013-01-17 06:36:42

标签: sql sql-server tsql reporting-services sql-job

我正在努力解决有关SQL Server作业历史的一些问题,这些问题是过去几天没有运气的。我喜欢在日志文件查看器中显示作业历史数据。我运行查询并获取数据,但我很困惑如何知道哪个步骤属于特定时间运行的作业。假设我们每小时都有一个作业,并且它有4个步骤,所以当每个作业运行时,它将在sysjobhistory中插入5条记录以便成功运行,现在我担心哪些步骤属于特定时间运行的作业。如果我想创建一个,我如何在深入报告中显示它。

SELECT sysjobhistory.server,
         sysjobs.name
         AS
         job_name,
         CASE sysjobhistory.run_status
           WHEN 0 THEN 'Failed'
           WHEN 1 THEN 'Succeeded'
           ELSE '???'
         END
         AS
         run_status,
         Isnull(Substring(CONVERT(VARCHAR(8), run_date), 1, 4) + '-' +
                       Substring(CONVERT(VARCHAR
                                 (8), run_date), 5, 2) + '-' +
                Substring(CONVERT(VARCHAR(
                          8), run_date), 7, 2), '')
         AS
         [Run DATE],
         Isnull(Substring(CONVERT(VARCHAR(7), run_time+1000000), 2, 2) + ':'
                 +
                       Substring(CONVERT(VARCHAR(7), run_time+1000000), 4, 2
                        )
                +
                ':' +
                Substring(CONVERT(VARCHAR(7), run_time+1000000), 6, 2), '')
         AS
         [Run TIME],
         Isnull(Substring(CONVERT(VARCHAR(7), run_duration+1000000), 2, 2) +
                 ':' +
                       Substring(CONVERT(VARCHAR(7), run_duration+1000000),
                       4,
                       2)
                + ':' +
                Substring(CONVERT(VARCHAR(7), run_duration+1000000), 6, 2),
         ''
         ) AS
         [Duration],
         sysjobhistory.step_id,
         sysjobhistory.step_name,
         sysjobhistory.MESSAGE
  FROM   msdb.dbo.sysjobhistory
         INNER JOIN msdb.dbo.sysjobs
           ON msdb.dbo.sysjobhistory.job_id = msdb.dbo.sysjobs.job_id

  ORDER  BY instance_id DESC

1 个答案:

答案 0 :(得分:1)

您可以尝试此查询。它基于step_id = 0创建临时作业表,为每个记录分配唯一标识符。然后,它使用运行时间和持续时间连接回作业历史记录表。因此,一个作业的所有步骤都具有相同的RUN_INSTANCE值。

-- create a temporary table of instances when a job was initiated
declare @JOBS table
(
    RUN_INSTANCE uniqueidentifier, 
    job_id uniqueidentifier,
    name sysname,
    run_status int, 
    run_date int, 
    run_time int, 
    run_duration int
);

-- insert one record for each instanced job and assign it a unique identifier
insert into @JOBS
    select 
        RUN_INSTANCE = NewID(), 
        h.job_id, 
        j.name, 
        h.run_status, 
        h.run_date, 
        h.run_time, 
        h.run_duration
    from msdb.dbo.sysjobhistory h
        join msdb.dbo.sysjobs j on j.job_id = h.job_id
    where step_id = 0

-- query the jobs history
select 
    h.server,
    j.RUN_INSTANCE, 
    j.name, 
    h.step_id, 
    h.run_date, 
    h.run_time, 
    run_status = 
        case h.run_status
            when 0 then 'failed'
            when 1 then 'succeeded'
            when 2 then 'retry'
            when 3 then 'canceled'
            when 4 then 'in progress'
            else '???'
        end,
    h.message
from @JOBS j
    join msdb.dbo.sysjobhistory h on 
        h.job_id = j.job_id
        and convert(varchar(20),h.run_date) + convert(varchar(20),h.run_time) 
           between convert(varchar(20),j.run_date) + convert(varchar(20),j.run_time) 
           and convert(varchar(20),j.run_date) + convert(varchar(20),j.run_time + j.run_duration) 
order by j.RUN_INSTANCE, h.step_id