3

我正在尝试解决有关过去几天 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
4

1 回答 1

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  
于 2013-01-25T17:08:24.073 回答