SQL Server查询Job的SQL语句

1、查询Job的基本信息

SELECT
    [job].[job_id] AS N'作业唯一标识符'
   ,[job].[name] AS N'作业名称'
   ,[dp].[name] AS N'作业创建者'
   ,[cat].[name] AS N'作业类别'
   ,[job].[description] AS N'作业描述'
   , CASE [job].[enabled]
        WHEN 1 THEN N'是'
        WHEN 0 THEN N'否'
      END AS N'是否启用'
   ,[job].[date_created] AS N'作业创建日期'
   ,[job].[date_modified] AS N'作业最后修改日期'
   ,[sv].[name] AS N'作业运行服务器名称'
   ,[step].[step_id] AS N'作业起始步骤'
   ,[step].[step_name] AS N'步骤名称'
   , CASE
        WHEN [sch].[schedule_uid] IS NULL THEN N'否'
          ELSE N'是'
      END AS N'是否分布式作业'
   ,[sch].[schedule_uid] AS N'作业计划的唯一标识符'
   ,[sch].[name] AS N'作业计划的用户定义名称'
   , CASE [job].[delete_level]
        WHEN 0 THEN N'不删除'
        WHEN 1 THEN N'成功后删除'
        WHEN 2 THEN N'失败后删除'
        WHEN 3 THEN N'完成后删除'
      END AS N'作业完成删除选项'
FROM [msdb].[dbo].[sysjobs] AS [job]
LEFT JOIN [msdb].[sys].[servers] AS [sv] ON [job].[originating_server_id] = [sv].[server_id]
LEFT JOIN [msdb].[dbo].[syscategories] AS [cat] ON [job].[category_id] = [cat].[category_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [step] ON [job].[job_id] = [step].[job_id] AND [job].[start_step_id] = [step].[step_id]
LEFT JOIN [msdb].[sys].[database_principals] AS [dp] ON [job].[owner_sid] = [dp].[sid]
LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [jsch] ON [job].[job_id] = [jsch].[job_id]
LEFT JOIN [msdb].[dbo].[sysschedules] AS [sch] ON [jsch].[schedule_id] = [sch].[schedule_id]
Where [job].[enabled] = 1 and [cat].[name]='ABC'
ORDER BY [job].[name]

2、查询Job的执行情况

SELECT
    [job].[job_id] AS N'作业唯一标示符'
   ,[job].[name] AS N'作业名称'
   , CASE WHEN [jobh].[run_date] IS NULL OR [jobh].[run_time] IS NULL THEN NULL
          ELSE CAST(CAST([jobh].[run_date] AS CHAR(8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([jobh].[run_time] AS VARCHAR (6)),6),3,0,':'),6,0,':') AS DATETIME)
     END AS N'最后执行时间'
   , CASE [jobh].[run_status]
      WHEN 0 THEN N'失败'
      WHEN 1 THEN N'成功'
      WHEN 2 THEN N'重试'
      WHEN 3 THEN N'取消'
      WHEN 4 THEN N'正在运行'
     END AS N'最后执行状态'
   ,STUFF(STUFF(RIGHT('000000'+CAST([jobh].[run_duration] AS VARCHAR(6)),6),3,0,':'),6,0,':') AS N'最后运行持续时间'
   ,[jobh].[message] AS N'最后运行状态信息'
   , CASE [jsch].[NextRunDate]
      WHEN 0 THEN NULL
      ELSE CAST(CAST([jsch].[NextRunDate] AS CHAR (8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([jsch].[NextRunTime] AS VARCHAR(6)),6),3,0,':'),6,0,':') AS DATETIME)
     END AS N'下次运行时间'
FROM [msdb].[dbo].[sysjobs] AS [job]
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 [jsch] ON [job].[job_id] = [jsch].[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 [jobh] ON [job].[job_id] = [jobh].[job_id] AND [jobh].[RowNumber] = 1
LEFT JOIN [msdb].[dbo].[syscategories] AS [cat] ON [job].[category_id] = [cat].[category_id]
Where [job].[enabled] = 1 and [cat].[name]='ABC'
ORDER BY [jobh].[run_date],[jobh].[run_time]

3、查询Job步骤基本信息

SELECT
    [job].[job_id] AS N'作业唯一标识符'
   ,[job].[name] AS N'作业名称'
   ,[jstep].[step_uid] AS N'步骤唯一标识符'
   ,[jstep].[step_id] AS N'步骤编号'
   ,[jstep].[step_name] AS N'步骤名称'
   ,CASE [jstep].[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 [jstep].[subsystem]
    END AS N'作业步骤类型'
   ,CASE
        WHEN [px].[name] IS NULL THEN N'SQL SERVER代理服务账户'
        ELSE [px].[name]
    END AS N'步骤运行账户'
   ,[jstep].[database_name] AS N'执行数据库名'
   ,[jstep].[command] AS N'执行命令'
   ,CASE [jstep].[on_success_action]
      WHEN 1 THEN N'退出报表成功的作业'
      WHEN 2 THEN N'退出报告失败的作业'
      WHEN 3 THEN N'转到下一步'
      WHEN 4 THEN N'转到步骤: ' + QUOTENAME(CAST([jstep].[on_success_step_id] AS VARCHAR(3))) + ' ' + [sOSSTP].[step_name]
    END AS N'执行成功后操作'
   ,[jstep].[retry_attempts] AS N'失败时的重试次数'
   ,[jstep].[retry_interval] AS N'重试间的等待时间(分钟)'
   ,CASE [jstep].[on_fail_action]
      WHEN 1 THEN N'退出报告成功的作业'
      WHEN 2 THEN N'退出报告失败的作业'
      WHEN 3 THEN N'转到下一步'
      WHEN 4 THEN '转到步骤: ' + QUOTENAME(CAST([jstep].[on_fail_step_id] AS VARCHAR(3))) + ' ' + [sOFSTP].[step_name]
    END AS N'执行失败后操作'
FROM [msdb].[dbo].[sysjobsteps] AS [jstep]
INNER JOIN [msdb].[dbo].[sysjobs] AS [job] ON [jstep].[job_id] = [job].[job_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOSSTP] ON [jstep].[job_id] = [sOSSTP].[job_id] AND [jstep].[on_success_step_id] = [sOSSTP].[step_id]
LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sOFSTP] ON [jstep].[job_id] = [sOFSTP].[job_id] AND [jstep].[on_fail_step_id] = [sOFSTP].[step_id]
LEFT JOIN [msdb].[dbo].[sysproxies] AS [px] ON [jstep].[proxy_id] = [px].[proxy_id] --代理账户信息
LEFT JOIN [msdb].[dbo].[syscategories] AS [cat] ON [job].[category_id] = [cat].[category_id]
Where [job].[enabled] = 1 and [cat].[name]='ABC'
ORDER BY [job].[name], [jstep].[step_id]

4、查询Job步骤执行情况

SELECT
    [job].[job_id] AS N'作业唯一标识符'
   ,[job].[name] AS N'作业名称'
   ,[jstep].[step_uid] AS N'作业步骤唯一标识符'
   ,[jstep].[step_id] AS N'步骤编号'
   ,[jstep].[step_name] AS N'步骤名称'
   ,CASE [jstep].[last_run_outcome]
      WHEN 0 THEN N'失败'
      WHEN 1 THEN N'成功'
      WHEN 2 THEN N'重试'
      WHEN 3 THEN N'取消'
      WHEN 5 THEN N'未知'
    END AS N'上次运行状态'
   ,STUFF(STUFF(RIGHT('000000' + CAST([jstep].[last_run_duration] AS VARCHAR(6)),6),3,0,':'),6,0,':') AS [LastRunDuration (HH:MM:SS)]
   ,[jstep].[last_run_retries] AS N'上次运行重复执行次数'
   ,CASE [jstep].[last_run_date]
      WHEN 0 THEN NULL
      ELSE CAST(CAST([jstep].[last_run_date] AS CHAR(8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([jstep].[last_run_time] AS VARCHAR(6)),6),3,0,':'),6,0,':') AS DATETIME)
    END AS N'上次运行时间'
FROM [msdb].[dbo].[sysjobsteps] AS [jstep]
INNER JOIN [msdb].[dbo].[sysjobs] AS [job] ON [jstep].[job_id] = [job].[job_id]
LEFT JOIN [msdb].[dbo].[syscategories] AS [cat] ON [job].[category_id] = [cat].[category_id]
Where [job].[enabled] = 1 and [cat].[name]='ABC'
ORDER BY [job].[name], [jstep].[step_id]