SQLSERVER2008 JOB信息查询

SQL中一旦job比较多,里面执行的存储过程,就不知道是有哪些,下列语句可以用来查询Job的存储过程,将'%msdb%'替换成你要查的存储过程名称

SELECT b.[name] [Job名称],CASE WHEN b.enabled=1 THEN '启用' ELSE '禁用' END [是否启用]
,b.description [Job描述],a.step_name [步骤名称],a.command [执行语句]
FROM msdb.dbo.sysjobsteps a INNER JOIN
msdb.dbo.sysjobs b ON a.job_id=b.job_id
WHERE a.command LIKE '%msdb%'

查找最新的JOB运行状况

SELECT
   j.[name] AS [JobName],
   j.enabled as enabled,
   run_status = CASE h.run_status
   WHEN 0 THEN 'Failed'
   WHEN 1 THEN 'Succeeded'
   WHEN 2 THEN 'Retry'
   WHEN 3 THEN 'Canceled'
   WHEN 4 THEN 'In progress'
   END,
   h.run_date AS LastRunDate, 
   h.run_time AS LastRunTime
FROM msdb.dbo.sysjobhistory h
   INNER JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
       WHERE
       j.enabled = 1   AND
       h.instance_id IN
       (SELECT MAX(h.instance_id)
           FROM msdb.dbo.sysjobhistory h GROUP BY (h.job_id));

你可能感兴趣的:(Sqlserver)