首页 > 代码库 > 作业查询

作业查询

1、作业所有者

1 --作业所有者2 select sj.name,sj.date_created,sj.date_modified,ssp.name OwnName from msdb.dbo.sysjobs sj3 left join sys.server_principals ssp4 on sj.owner_sid=ssp.sid5 --where ssp.name=‘loginname‘6 order by sj.name

2、禁用、调度未启用的作业

 1 --禁用、调度未启用的作业 2 select case when a.enabled=0 then JobDisabled 3   when c.enabled=0 then ScheduleDisabled end as DisabledType 4 ,a.name,a.date_created,a.date_modified from msdb.dbo.sysjobs a 5 inner join msdb.dbo.sysjobschedules b 6 on a.job_id=b.job_id 7 inner join msdb.dbo.sysschedules c 8 on b.schedule_id=c.schedule_id 9 where a.enabled=0 or c.enabled=010 order by a.enabled,a.name

3、历史记录(代理-属性-历史记录,可设置总日志最大行数和单个作业最大行数)查看,作业最早执行时间、最晚执行时间、执行次数,结合作业步骤数可大致知晓是否能继续保存新的日志记录且不删除旧的日志记录。

 1 --作业执行历史记录 2 select a.*,a.JobRunTimes*(b.StepCount+1) HistoryRecords from( 3 SELECT sj.name,COUNT(sj.name) JobRunTimes 4 ,min(msdb.dbo.agent_datetime(run_date,run_time)) FirstRunDateTime--05及以上版本可直接调用msdb.dbo.agent_datetime 5 ,max(msdb.dbo.agent_datetime(run_date,run_time)) LastRunDateTime 6 FROM  msdb.dbo.sysjobhistory sjh 7 INNER JOIN msdb.dbo.sysjobs sj 8 ON sjh.job_id=sj.job_id 9 WHERE sjh.step_id=0 AND sj.enabled=110 GROUP BY sj.name,sj.job_id) a11 inner join (12 select sj.name,COUNT(sjs.step_id) StepCount from msdb.dbo.sysjobs sj13 inner join msdb.dbo.sysjobsteps sjs14 on sj.job_id=sjs.job_id15 group by sj.name) b16 on a.name=b.name17 order by a.FirstRunDateTime

 

作业查询