Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66359
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-06 11:56
个人简介

勤奋、刻苦,坚持不懈的精神!

文章分类

全部博文(56)

文章存档

2013年(56)

我的朋友

分类: SQLServer

2013-11-06 12:03:37

原文地址:SQLServer:SQL中JOB的运行状态 作者:hnynes

 一, 查看是否正在运行
  Declare @Job_ID as UNIQUEIDENTIFIER
  select @Job_ID =Job_ID from msdb.dbo.sysjobs where name = 'James_Test'
  Exec master..sp_MSget_jobstate @Job_ID
  返回值为 1 - 正在运行
  4 - 表示完成(成功或失败)
  二, 检查执的结果状态
  查看 SQL Server 作业(job) 运行成功与否、还是取消等状态信息,以及作业最后一次运行持续时间、出错信息提示等。DBA 可以周期性地调度该 SQL 脚本,在 SQL Server 作业失败时候得到及时提醒。此 SQL 脚本适用于 SQL Server 2000 & 2005。
  -- author : p.c.w.l
  -- source :
  -- create : 2008-11-27
  -- descr : a simple sql script to view sql server jobs run status
  select category = jc.name,
  category_id = jc.category_id,
  job_name = j.name,
  job_enabled = j.enabled,
  last_run_time = cast(js.last_run_date as varchar(10)) + '-' + cast(js.last_run_time as varchar(10)),
  last_run_duration = js.last_run_duration,
  last_run_status = js.last_run_outcome,
  last_run_msg = js.last_outcome_message + cast(nullif(js.last_run_outcome,1) as varchar(2)),
  job_created = j.date_created,
  job_modified = j.date_modified
  from msdb.dbo.sysjobs j
  inner join msdb.dbo.sysjobservers js
  on j.job_id = js.job_id
  inner join msdb.dbo.syscategories jc
  on j.category_id = jc.category_id
  where j.enabled = 1
  and js.last_run_outcome in (0,1,3,5) -- 0:Fail 1:Succ 3:Cancel 5:First run

  and jc.category_id not between 10 and 20 -- repl


/*
   category_id name
   0  [Uncategorized (Local)]
   1  Jobs from MSX
   2  [Uncategorized (Multi-Server)]
   3  Database Maintenance
   4  Web Assistant
   5  Full-Text
   6  Log Shipping
   7  Database Engine Tuning Advisor
   10 REPL-Distribution
   11 REPL-Distribution Cleanup
   12 REPL-History Cleanup
   13 REPL-LogReader
   14 REPL-Merge
   15 REPL-Snapshot
   16 REPL-Checkup
   17 REPL-Subscription Cleanup
   18 REPL-Alert Response
   19 REPL-QueueReader
   20 Replication
   98 [Uncategorized]
   99 [Uncategorized]
*/

  三, 查看历史情况

  select run_status,a.run_time, a.* from msdb.dbo.sysjobhistory a inner join msdb.dbo.sysjobs b on a.job_id=b.job_id where b.name='作业名'作业的执行状态run_status: 0 = 失败1 = 成功2 = 重试3 = 取消4 = 正在进行。


原文地址:

http://blog.csdn.net/chenghaibing2008/article/details/6064714


阅读(1861) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~