2013-01-17 6 views
3

지난 며칠 동안 SQL Server 작업 기록과 관련된 문제를 해결하려고합니다. 로그 파일 뷰어에서 볼 수 있듯이 작업 기록 데이터를 표시하고 싶습니다. 쿼리를 실행하고 데이터를 얻지 만 특정 단계에서 실행중인 작업에 속한 단계를 파악하는 방법에 대해 혼란스러워합니다. 우리는 매 시간마다 작업을 실행하고 4 단계를 수행하면 모든 작업이 실행될 때 성공적인 실행을 위해 sysjobhistory에 5 개의 레코드를 삽입 할 것이므로 특정 단계에서 실행중인 작업이 어떤 단계에 속해 있는지 염려합니다. 하나를 만들려는 경우 드릴 다운 보고서에 어떻게 표시 할 수 있습니까?SQL Server의 작업 내역

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

이 쿼리를 시도해 볼 수 있습니다. step_id = 0에 기초한 작업의 임시 테이블을 작성하여 각 레코드에 고유 ID를 지정합니다. 그런 다음 런타임 및 기간을 사용하여 작업 기록 테이블에 다시 조인합니다. 따라서 한 작업의 모든 단계는 동일한 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