2015-06-11 83 views
1

我在SQL Server R2中安排了很多作業。 每個作業可能包含幾個步驟。SQL Server作業歷史2008 R2

我希望爲每個工作項目下的每個工作階段提供lastrundate,lastrunstatus,lastrunduration,lastrunmessage,nextrundate。

我有這個查詢與我工作正常。

唯一的問題是,它沒有讓我的LastRunDateTime字符串時間值轉換

SELECT 
[sJOB].[job_id] AS [JobID], 
[sJOB].[name] AS [JobName], 
[sJOBS].step_name AS [StepName], 
CASE 
    WHEN [sJOBS].[last_run_date] IS NULL OR [sJOBS].[last_run_time] IS NULL THEN NULL 
    ELSE 
      CAST(CAST([sJOBS].[last_run_date] AS VARCHAR(8)) 
      + ' ' 
      + STUFF(
       STUFF(RIGHT('000000' + CAST([sJOBS].[last_run_time] AS VARCHAR(6)), 6) 
        , 3, 0, ':') 
       , 6, 0, ':') AS DATETIME) 
    END AS [LastRunDateTime], 
CASE [sJOBH].[run_status] 
    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], 
    STUFF(
     STUFF(RIGHT('000000' + CAST([sJOBS].[last_run_duration] AS VARCHAR(6)), 6) 
      , 3, 0, ':') 
     , 6, 0, ':') 
    AS [LastRunDuration (HH:MM:SS)], 
    [sJOBH].[message] AS [LastRunStatusMessage], 
    CASE [sJOBSCH].[NextRunDate] 
    WHEN 0 THEN NULL 
    ELSE CAST(
      CAST([sJOBSCH].[NextRunDate] AS CHAR(8)) 
      + ' ' 
      + STUFF(
       STUFF(RIGHT('000000' + CAST([sJOBSCH].[NextRunTime] AS VARCHAR(6)), 6) 
        , 3, 0, ':') 
       , 6, 0, ':') 
      AS DATETIME) 
    END AS [NextRunDateTime] 
FROM 
(SELECT * FROM [msdb].[dbo].[sysjobs] WHERE enabled = 1) AS [sJOB] 
LEFT JOIN sysjobsteps [sJOBS] 
     ON([sJOB].[job_id] = [sJOBS].[job_id]) 
LEFT JOIN (
      SELECT 
       [job_id], 
       MIN([next_run_date]) AS [NextRunDate], 
       MIN([next_run_time]) AS [NextRunTime] 
      FROM [msdb].[dbo].[sysjobschedules] 
      GROUP BY [job_id] 
     ) AS [sJOBSCH] 
    ON [sJOB].[job_id] = [sJOBSCH].[job_id] 
LEFT JOIN (
      SELECT 
       [job_id], 
       [run_date], 
       [run_time], 
       [run_status], 
       [run_duration], 
       [message], 
       ROW_NUMBER() OVER (
             PARTITION BY [job_id] 
             ORDER BY [run_date] DESC, [run_time] DESC 
       ) AS RowNumber 
      FROM [msdb].[dbo].[sysjobhistory] 
      WHERE [step_id] = 0 
     ) AS [sJOBH] 
    ON [sJOB].[job_id] = [sJOBH].[job_id] 
    AND [sJOBH].[RowNumber] = 1 
ORDER BY [JobName] 

上LastRunDateTime的CAST()函數給了我下面的錯誤

Msg 241, Level 16, State 1, Line 1 
Conversion failed when converting date and/or time from character string. 

誰能告訴我什麼是錯的?

+0

你可以嘗試縮小它,並提供一個更小的repro?我不認爲你需要所有這些龐大的查詢,你已經知道哪些列導致了這個問題。然後,向我們顯示查詢,以及查詢的字符串結果,而不是CAST。 – Greg

回答

1

不能保證你的問題完全一樣,但在我測試過的系統上,我得到了同樣的錯誤,因爲某些作業步驟的0值爲last_run_date

我修復了以下對您的查詢的修改。也許這也適用於你。

... 
CASE 
    WHEN [sJOBS].[last_run_date] IS NULL OR [sJOBS].[last_run_time] IS NULL THEN NULL 
    WHEN [sJOBS].[last_run_date] = 0 THEN NULL --<< line added here << 
    ELSE 
      CAST(CAST([sJOBS].[last_run_date] AS VARCHAR(8)) 
      + ' ' 
      + STUFF(
       STUFF(RIGHT('000000' + CAST([sJOBS].[last_run_time] AS VARCHAR(6)), 6) 
        , 3, 0, ':') 
       , 6, 0, ':') AS DATETIME) 
    END AS [LastRunDateTime], 
... 
+0

非常好!我沒有看到那個來! :) 我將你的行添加到我的OR條件列表中 WHEN [sJOBS]。[last_run_date] IS NULL OR [sJOBS]。[last_run_time] IS NULL OR [sJOBS]。[last_run_date] = 0 THEN NULL –