查询SqlServer代理作业(JOB)执行了哪些内容

转载自 https://blog.csdn.net/xt0522/article/details/104610532/
sql语句如下,可以根据自己需求添加筛选条件

SELECT
    [job].[job_id] AS '作业唯一标识符'
   ,[job].[name] AS '作业名称'
   ,[jstep].[step_uid] AS '步骤唯一标识符'
   ,[jstep].[step_id] AS '步骤编号'
   ,[jstep].[step_name] AS '步骤名称'
   ,[job].[description] as '作业描述'
   ,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 '作业步骤类型'
   ,CASE
        WHEN [px].[name] IS NULL THEN 'SQL SERVER代理服务账户'
        ELSE [px].[name]
    END AS '步骤运行账户'
   ,[jstep].[database_name] AS '执行数据库名'
   ,[jstep].[command] AS '执行命令'
   ,CASE [jstep].[on_success_action]
      WHEN 1 THEN '退出报表成功的作业'
      WHEN 2 THEN '退出报告失败的作业'
      WHEN 3 THEN '转到下一步'
      WHEN 4
      THEN '转到步骤: '
           + QUOTENAME(CAST([jstep].[on_success_step_id] AS VARCHAR(3))) + ' '
           + [sOSSTP].[step_name]
    END AS '执行成功后操作'
   ,[jstep].[retry_attempts] AS '失败时的重试次数'
   ,[jstep].[retry_interval] AS '重试间的等待时间(分钟)'
   ,CASE [jstep].[on_fail_action]
      WHEN 1 THEN '退出报告成功的作业'
      WHEN 2 THEN '退出报告失败的作业'
      WHEN 3 THEN '转到下一步'
      WHEN 4
      THEN '转到步骤: '
           + QUOTENAME(CAST([jstep].[on_fail_step_id] AS VARCHAR(3))) + ' '
           + [sOFSTP].[step_name]
    END AS '执行失败后操作'
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]
--查询条件
--WHERE [jstep].[database_name] = 'AMES' AND[jstep].[command] like '%RPT_QUALITY_PRODUCT_DATA%'
ORDER BY [job].[name], [jstep].[step_id]
posted @ 2021-04-19 13:55  诡局  阅读(487)  评论(0编辑  收藏  举报