SQL SERVER 查詢作業(Job)基本資訊及執行情況的sql
阿新 • • 發佈:2019-01-02
SELECT
[job].[job_id] AS '作業唯一識別符號'
,[job].[name] AS '作業名稱'
,[jobstep].[step_uid] AS '步驟唯一識別符號'
,[jobstep].[step_id] AS '步驟編號'
,[jobstep].[step_name] AS '步驟名稱'
,CASE [jobstep].[subsystem]
WHEN 'ActiveScripting' THEN 'ActiveX Script'
WHEN 'CmdExec' THEN 'Operating system (CmdExec)'
WHEN 'PowerShell' THEN 'PowerShell'
WHEN 'Distribution' THEN 'Replication Distributor'
WHEN 'Merge' THEN 'Replication Merge'
WHEN 'QueueReader' THEN 'Replication Queue Reader'
WHEN 'Snapshot' THEN 'Replication Snapshot'
WHEN 'LogReader' THEN 'Replication Transaction-Log Reader'
WHEN 'ANALYSISCOMMAND' THEN 'SQL Server Analysis Services Command'
WHEN 'ANALYSISQUERY' THEN 'SQL Server Analysis Services Query'
WHEN 'SSIS' THEN 'SQL Server Integration Services Package'
WHEN 'TSQL' THEN 'Transact-SQL script (T-SQL)'
ELSE [jobstep].[subsystem]
END AS '作業步驟型別'
,CASE
WHEN [px].[name] IS NULL THEN 'SQL SERVER代理服務賬戶'
ELSE [px].[name]
END AS '步驟執行賬戶'
,[jobstep].[database_name] AS '執行資料庫名'
,[jobstep].[command] AS '執行命令'
,CASE [jobstep].[on_success_action]
WHEN 1 THEN '退出報表成功的作業'
WHEN 2 THEN '退出報告失敗的作業'
WHEN 3 THEN '轉到下一步'
WHEN 4
THEN '轉到步驟: '
+ QUOTENAME(CAST([jobstep].[on_success_step_id] AS VARCHAR(3))) + ' '
+ [sOSSTP].[step_name]
END AS '執行成功後操作'
,[jobstep].[retry_attempts] AS '失敗時的重試次數'
,[jobstep].[retry_interval] AS '重試間的等待時間(分鐘)'
,CASE [jobstep].[on_fail_action]
WHEN 1 THEN '退出報告成功的作業'
WHEN 2 THEN '退出報告失敗的作業'
WHEN 3 THEN '轉到下一步'
WHEN 4
THEN '轉到步驟: '
+ QUOTENAME(CAST([jobstep].[on_fail_step_id] AS VARCHAR(3))) + ' '
+ [sOFSTP].[step_name]
END AS '執行失敗後操作'
FROM [msdb].[dbo].[sysjobsteps] AS [jobstep]
INNER JOIN [msdb].[dbo].[sysjobs] AS [job]
ON [jobstep].[job_id] = [job].[job_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOSSTP]
ON [jobstep].[job_id] = [sOSSTP].[job_id]
AND [jobstep].[on_success_step_id] = [sOSSTP].[step_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOFSTP]
ON [jobstep].[job_id] = [sOFSTP].[job_id]
AND [jobstep].[on_fail_step_id] = [sOFSTP].[step_id]
LEFT JOIN [msdb].[dbo].[sysproxies] AS [px]--代理賬戶資訊
ON [jobstep].[proxy_id] = [px].[proxy_id]
WHERE [jobstep].[database_name]='資料庫的名稱'
AND UPPER([jobstep].[command]) LIKE UPPER('%執行的命令名稱%')
ORDER BY [job].[name], [jobstep].[step_id]