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,如需转载请自行联系原作者