2017-09-29 51 views
-1

我需要一個Schedule_run_id用於查詢,但我沒有找到。SQL Server列出來自一個jobrun的所有步驟

我已經嘗試過這樣的:

SELECT [sSCH].[schedule_uid], 
h.step_id, 
h.run_status, 
j.name, 
REPLACE(TimeValueToTime(NULL,h.run_date),':','-') + ' ' + 
TimeValueToTime(NULL,h.run_time) AS RUNTIME, 
fnTimeValueToTime(NULL,h.run_duration) AS DURATION 
    FROM  msdb.dbo.sysjobhistory AS h 
    INNER JOIN msdb.dbo.sysjobs AS j ON h.job_id = j.job_id 
     LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH] 
     ON sJOBSCH.job_id = j.job_id 
    LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH] 
     ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id] 
    WHERE h.step_id != 0 AND 
j.name IN ('Job') 

的Schedule_uid始終是相同的。每次運行需要一個特定的ID。

感謝您的幫助!

回答

0

如果你想要特定的ID如計數器,你可以創建一個只有IDENTITY ID的臨時表,並在查詢運行時插入。或者,如果您需要此特定ID的標識列,您可以在select語句中使用NEWID()。這會給你一個GUID。我正在添加示例查詢;

CREATE TABLE #temp(
ID INT IDENTITY(1,1) 
) 


INSERT INTO #temp DEFAULT VALUES 

SELECT (SELECT COUNT(*) FROM #temp) AS ID, 
h.step_id, 
h.run_status, 
j.name 
    FROM  msdb.dbo.sysjobhistory AS h 
    INNER JOIN msdb.dbo.sysjobs AS j ON h.job_id = j.job_id 
     LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH] 
     ON sJOBSCH.job_id = j.job_id 
    LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH] 
     ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id] 
    WHERE h.step_id != 0 AND 
j.name IN ('Job') 

SELECT NEWID() AS ID, 
h.step_id, 
h.run_status, 
j.name 
    FROM  msdb.dbo.sysjobhistory AS h 
    INNER JOIN msdb.dbo.sysjobs AS j ON h.job_id = j.job_id 
     LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH] 
     ON sJOBSCH.job_id = j.job_id 
    LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH] 
     ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id] 
    WHERE h.step_id != 0 AND 
j.name IN ('Job') 
+0

謝謝,但多數民衆贊成不是我想要的 - >問題:當作業失敗並重試成功,解決方案給出2個不同的ID。具體化 - >我想要的是某種Jobhistory(右鍵單擊SSMS中的歷史記錄)作爲表格。 – Meitoasty

相關問題