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
),所以上网查了一下,呵呵
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
相关文章推荐
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- [Yii Framework] How to run cron job (or use command) in the server with yii framework
- How to move the databases that are used by SharePoint Portal Server 2003 to a computer that is running SQL Server
- 海洋工作室——网站建设专家:How to check the SQL statement execute time on SQL Server ?
- How to run an existing SQL Server Agent Job from C#
- [转]How to find who is using / eating up the Virtual Address Space on your SQL Server
- Tip/Trick: How to Run a Root “/” Site with the Local Web Server using VS 2005 SP1
- How to automatic process SSAS cube using SQL Server agent job
- How to run the .net code within the Sql server
- ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- Using the DBMS_SQLTUNE package to Run the Sql Tuning Advisor
- How to Change the DBID and the DBNAME by using NID [ID 224266.1]
- SQL: How to get a series of dates in sql using connect by
- How to see all the application errors related to SQL Server and export them to a .csv file.
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- [monitoring] how to create script to monitoring the status of orders and make graphs
- Using Reporting Services to Search the SQL Server Log
- How to check Fragmentation on SQL Server 2005
- Step by Step Guide to Add a SQL Job in SQL Server 2005