如何获取SQL Server

时间:2015-07-03 06:25:07

标签: sql sql-server ssis

我必须按照与计划时相同的步骤顺序获取每个作业中的所有步骤以及更多数据在特定日期,如下所示:

JobName,StepName,LastRunDate,LastRunDuration,LastRunStatus,LastRunStatusMsg

我目前所拥有的东西给了我我想要的东西,但它并没有给我所有的步骤。有时,它给了我所有乔布斯的所有步骤,有时候只有很少的工作。

这是我的查询:

SELECT  JobName ,
        StepName ,
        LastRunDateTime ,
        [LastRunDuration (HH:MM:SS)] ,
        LastRunStatus ,
        LastRunStatusMsg
FROM    ( SELECT    j.name AS JobName ,
                    s.step_name AS StepName ,
                    s.step_id ,
                    LastRunDateTime = msdb.dbo.agent_datetime(CASE
                                                              WHEN s.last_run_date = 0
                                                              THEN NULL
                                                              ELSE s.last_run_date
                                                              END,
                                                              CASE
                                                              WHEN s.last_run_time = 0
                                                              THEN NULL
                                                              ELSE s.last_run_time
                                                              END) ,
                    STUFF(STUFF(RIGHT('000000'
                            + CAST(s.last_run_duration AS VARCHAR(6)),
                            6), 3, 0, ':'), 6, 0, ':') AS 'LastRunDuration (HH:MM:SS)' ,
                    CASE s.last_run_outcome
                      WHEN 0 THEN 'Failed'
                      WHEN 1 THEN 'Succeeded'
                      WHEN 2 THEN 'Retry'
                      WHEN 3 THEN 'Canceled'
                      WHEN 4 THEN 'Running' -- In Progress
                    END AS LastRunStatus ,
                    REPLACE(REPLACE(REPLACE(REPLACE(h.message, CHAR(9), ''),
                                            CHAR(10), ''), CHAR(12), ''),
                            CHAR(13), '') AS LastRunStatusMsg
          FROM      sysjobs j
                    JOIN msdb.dbo.sysjobsteps s ON j.job_id = s.job_id
                    JOIN sysjobhistory h ON s.job_id = h.job_id
                                            AND s.step_id = h.step_id
                                            AND s.last_run_date = h.run_date
                                            AND s.last_run_time = h.run_time
        ) MainQ
WHERE   CONVERT(DATE, MainQ.LastRunDateTime) = CONVERT(DATE, '06/16/2015')
ORDER BY MainQ.JobName , MainQ.LastRunDateTime

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

select 
jt.JOBTYPE As JobType,
j.name As JobName,
s.step_name As StepName,
msdb.dbo.agent_datetime(run_date, run_time) As RunDateTime,
STUFF(STUFF(RIGHT('000000' + CAST(s.last_run_duration AS VARCHAR(6)),  6), 3, 0, ':'), 6, 0, ':')
AS 'LastRunDuration (HH:MM:SS)',
CASE s.last_run_outcome
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'Running' -- In Progress
END AS LastRunStatus,
Replace(Replace(Replace(Replace(h.message, Char(9), ''), Char(10), ''), Char(12), ''), Char(13), '') As LastRunStatusMsg
From msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.sysjobsteps s
ON j.job_id = s.job_id
INNER JOIN msdb.dbo.sysjobhistory h
ON s.job_id = h.job_id 
AND s.step_id = h.step_id 
AND h.step_id <> 0
and Convert(Date, msdb.dbo.agent_datetime(run_date, run_time)) = CONVERT(Date, GETDATE())
Left JOIN BASE..JOB_TYPE jt
ON j.name=jt.JOBNAME
where j.enabled = 1   --Only Enabled Jobs