zoukankan      html  css  js  c++  java
  • How to check the MS SQL Server job run status by using Script

    因为想做一个每天定时去各个服务器上检查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

  • 相关阅读:
    RollingFileAppender 日志按天记录输出到log文件
    spring boot jar包开机自启
    oracle 日常操作--触发器调试
    转载:java基础-String不可变的好处
    java 代码优化
    nginx部署ssl证书
    修改npm仓库地址
    linux下安装python3
    python一行代码开启http
    linux下安装mongodb
  • 原文地址:https://www.cnblogs.com/liangqihui/p/1566164.html
Copyright © 2011-2022 走看看