您的位置:首页 > 数据库

How to check the MS SQL Server job run status by using Script

2009-09-14 10:25 731 查看
因为想做一个每天定时去各个服务器上检查sql 日志是否正常运行的小东东(主要还是懒,不想满世界登陆数据库去看日志

),所以上网查了一下,呵呵

SELECT a.name as job, left(b.step_name, 20) as [step name],
CASE b.run_status WHEN 0 THEN 'Failed' WHEN 1 THEN 'Succeeded' WHEN 2 THEN 'Retry' WHEN 3 THEN 'Canceled' ELSE

'Running' END as status,
SUBSTRING(CAST(b.run_date AS CHAR(8)),5,2) + '/' + RIGHT(CAST(b.run_date AS CHAR(8)),2) + '/' + LEFT(CAST

(b.run_date AS CHAR(8)),4) as [date],
LEFT(RIGHT('000000' + CAST(b.run_time AS VARCHAR(10)),6),2) + ':' + SUBSTRING(RIGHT('000000' + CAST(b.run_time AS

VARCHAR(10)),6),3,2) + ':' + RIGHT(RIGHT('000000' + CAST(b.run_time AS VARCHAR(10)),6),2) as [time],
b.message as Err_message
From msdb..sysjobs a
INNER JOIN msdb..sysjobhistory b ON a.job_id = b.job_id
inner join (select job_id,max(instance_id) as maxinstance from msdb..sysjobhistory group by job_id) x
on a.job_id = x.job_id and b.instance_id = x.maxinstance --b.instance_id = x.maxinstance--最近一次运行结果
where a.enabled =1 and b.run_status <>1 and b.run_status <> 4 --查看运行失败以及异常取消的
ORDER BY job, convert(char, b.run_date,111)+convert(char,b.run_time,111) desc

本文中Sql语句来自CSDN博客,原出处为:http://blog.csdn.net/tdl982324/archive/2004/09/29/120739.aspx

BTW
For the run_status column of sysjobhistory, msdn gives the following statuses that can be returned:

Status of the job execution:
0 = Failed
1 = Succeeded
2 = Retry
3 = Canceled
4 = In progress
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐