天天看點

檢視Job執行的曆史記錄 檢視Job執行的曆史記錄

SQL Server将Job的資訊存放在msdb中,Schema是dbo,表名以“sysjob”開頭。

一,基礎表

1, 檢視Job和Step,Step_ID 是從1 開始的。

2, 檢視 特定job 的所有 Step的執行記錄,Step_id=0 記錄job的整體執行情況;run_time 和 run_duration 是int類型,格式是hhmmss。

3,Job History的查詢

4,通過msdb.dbo.sysjobsteps 檢視指定Job中每個step 最後執行的狀态

5,檢視每個Job最後一次執行的狀态和該job最後一個Step的執行資訊。

二,檢視Running jobs

Agent在運作時,會建立一個Session,并将current SessionID存儲在msdb.dbo.syssessions 中。Agent在執行每一個job時,都會将SessionID 和Job_ID 寫入 msdb.dbo.sysjobactivity 中,是以 msdb.dbo.sysjobactivity 記錄目前Agent 正在運作的每一個Job的資訊(Job開始執行的時間,執行成功的最後一個StepID....),如果要檢視Agent目前執行的Job,那麼msdb.dbo.sysjobactivity的SessionID必須是目前Agent使用的SessionID。

1,基礎表

msdb.dbo.syssessions 

Each time SQL Server Agent starts, it creates a new session. SQL Server Agent uses sessions to preserve the status of jobs when the SQL Server Agent service is restarted or stopped unexpectedly. Each row of the syssessions table contains information about one session. Use the sysjobactivity table to view the job state at the end of each session. Every time the agent is started a new session_id is added to the syssessions table.

msdb.dbo.sysjobactivity

Records current SQL Server Agent job activity and status. The column last_executed_step_id is the id of the last step completed.  If the job is on the first step it’s NULL.  So getting the current step is a simple formula of ISNULL(last_executed_step_id,0)+1.

2,檢視目前正在運作的Job

參考文檔:

<a href="https://sqlstudies.com/2013/09/05/a-t-sql-query-to-get-current-job-activity/">A T-SQL query to get current job activity</a>

<a href="https://msdn.microsoft.com/en-us/library/ms181367.aspx" target="_blank">SQL Server Agent Tables (Transact-SQL)</a>

本文版權歸作者和部落格園所有,歡迎轉載,但未經作者同意,必須保留此段聲明,且在文章頁面醒目位置顯示原文連接配接,否則保留追究法律責任的權利。

本文轉自悅光陰部落格園部落格,原文連結:http://www.cnblogs.com/ljhdo/p/5498433.html,如需轉載請自行聯系原作者

繼續閱讀