SQL Server - 获取作业历史记录详细信息

时间:2011-09-29 13:07:42

标签: sql sql-server

我们使用下面提到的存储过程来获取JOBS的历史记录

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,
             CAST(
             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 DATETIME)
             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],
             Isnull(Substring(CONVERT(VARCHAR(7), run_time+run_duration+1000000), 2, 2) + ':'
                     +
                           Substring(CONVERT(VARCHAR(7), run_time+run_duration+1000000), 4, 2
                            )
                    +
                    ':' +
                    Substring(CONVERT(VARCHAR(7), run_time+run_duration+1000000), 6, 2), '')
             AS
             [Total TIME],             
             sysjobhistory.step_id,
             sysjobhistory.step_name,
             sysjobhistory.MESSAGE AS Msg
      FROM   msdb.dbo.sysjobhistory
             INNER JOIN msdb.dbo.sysjobs
               ON msdb.dbo.sysjobhistory.job_id = msdb.dbo.sysjobs.job_id
      WHERE  sysjobhistory.run_date <= Datepart(yyyy, @dateparam) * 10000 +
                                             Datepart(mm, @dateparam) * 100 +
                                      Datepart
                                      (
                                             dd, @dateparam)
             AND sysjobs.name = @JobName --remove this line if you want to show all jobs for the specified day

但是,如果作业失败,任何人都可以建议如何获取错误详细信息(我们正在寻找可以通过日志查看器查看的信息)

Date        29/09/2011 07:57:04
Log     Job History Step ID     5
Server       
Job Name        
Step Name       extract
Duration        00:02:13
Sql Severity        0
Sql Message ID      0
Operator Emailed        
Operator Net sent       
Operator Paged      
Retries Attempted       0

信息 (也需要出现此错误消息)

1 个答案:

答案 0 :(得分:10)

您可以使用以下脚本获取每个步骤的详细信息。首先,您必须获取失败作业的job_id并传递给msdb.dbo.sp_help_jobhistory存储过程。

我通过在作业活动监视器上执行活动时运行跟踪来获得此脚本。

我希望这可以帮助你找到你想要的东西

要查找STEP级别消息,请查看message&lt;&gt;所在的step_id列。 0

这些记录实际上提供了有用的错误消息。

declare @tmp_sp_help_jobhistory table
(
    instance_id int null, 
    job_id uniqueidentifier null, 
    job_name sysname null, 
    step_id int null, 
    step_name sysname null, 
    sql_message_id int null, 
    sql_severity int null, 
    message nvarchar(4000) null, 
    run_status int null, 
    run_date int null, 
    run_time int null, 
    run_duration int null, 
    operator_emailed sysname null, 
    operator_netsent sysname null, 
    operator_paged sysname null, 
    retries_attempted int null, 
    server sysname null  
)

insert into @tmp_sp_help_jobhistory 
exec msdb.dbo.sp_help_jobhistory 
    @job_id = '329cac18-328d-499b-9216-593244d164b0', --change this job_id according to your requirement
    @mode='FULL' 

SELECT
    tshj.instance_id AS [InstanceID],
    tshj.sql_message_id AS [SqlMessageID],
    tshj.message AS [Message],
    tshj.step_id AS [StepID],
    tshj.step_name AS [StepName],
    tshj.sql_severity AS [SqlSeverity],
    tshj.job_id AS [JobID],
    tshj.job_name AS [JobName],
    tshj.run_status AS [RunStatus],
    CASE tshj.run_date WHEN 0 THEN NULL ELSE
    convert(datetime, 
            stuff(stuff(cast(tshj.run_date as nchar(8)), 7, 0, '-'), 5, 0, '-') + N' ' + 
            stuff(stuff(substring(cast(1000000 + tshj.run_time as nchar(7)), 2, 6), 5, 0, ':'), 3, 0, ':'), 
            120) END AS [RunDate],
    tshj.run_duration AS [RunDuration],
    tshj.operator_emailed AS [OperatorEmailed],
    tshj.operator_netsent AS [OperatorNetsent],
    tshj.operator_paged AS [OperatorPaged],
    tshj.retries_attempted AS [RetriesAttempted],
    tshj.server AS [Server],
    getdate() as [CurrentDate]
FROM @tmp_sp_help_jobhistory as tshj
ORDER BY [InstanceID] ASC