在SQL Server中判斷一個作業是否正在運行

來源:互聯網
上載者:User

select step_id,run_status,run_date As rundate
--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='PCDBI'
order by run_date DESC,step_id Asc

作業的執行狀態run_status:
0 = 失敗
1 = 成功
2 = 重試
3 = 取消
4 = 進行中
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- author : p.c.w.l
-- source : www.sqlstudy.com
-- create : 2008-01-01
-- 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]
*/

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.