目前我的情况是job分散在多个服务器,近400个JOB都是单独执行的,为了统计出所有JOB的执行计划,我分了四种情况,分别如下:每天执行一次的,每天循环执行的,按星期和按月份执行的四类。SQL 语句如下:
SELECT JOB.name,SysSche.name, substring(right(‘00‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘00‘+cast(active_start_time as varchar(6)),6),3,2) active_start_time, 215 server_name, ‘Occurs every day at ‘+ substring(right(‘00‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘00‘+cast(active_start_time as varchar(6)),6),3,2) +‘.Schedule will be used starting on ‘ +cast(active_start_date as varchar(10)) DescriptionStr FROM [dbo].[sysjobs] JOB LEFT JOIN [dbo].[sysjobschedules] SysJobSche ON JOB.job_id = SysJobSche.job_id LEFT JOIN [dbo].[sysschedules] SysSche ON SysJobSche.schedule_id=SysSche.schedule_id WHERE JOB.enabled=1 and SysSche.name is not null --20 and SysSche.freq_type = 4 and SysSche.freq_subday_type = 1 --- order by SysSche.freq_subday_interval
USE msdb GO
SELECT JOB.name JobName,SysSche.name ScheduleName, substring(right(‘000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘000‘+cast(active_start_time as varchar(6)),6),3,2) StartTime, substring(right(‘000‘+cast(active_end_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘000‘+cast(active_end_time as varchar(6)),6),3,2)+‘:‘ +substring(right(‘000‘+cast(active_end_time as varchar(6)),6),5,2) EndTime, freq_subday_interval,‘Hours‘ unit, ‘215‘ ServerName,‘Occurs every day every ‘+cast(freq_subday_interval as varchar(2))+‘ hour(s) between ‘+ cast(active_start_time as varchar(10))+‘ and ‘+cast(active_end_time as varchar(10))+‘ .Schedule will be used starting on ‘+ cast(active_start_date as varchar(10)) DescriptionStr FROM [dbo].[sysjobs] JOB LEFT JOIN [dbo].[sysjobschedules] SysJobSche ON JOB.job_id = SysJobSche.job_id LEFT JOIN [dbo].[sysschedules] SysSche ON SysJobSche.schedule_id=SysSche.schedule_id WHERE JOB.enabled=1 and SysSche.name is not null --20 and SysSche.freq_type = 4 and SysSche.freq_subday_type = 8 --- --order by SysSche.freq_subday_interval
union
SELECT JOB.name,SysSche.name, substring(right(‘000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘000‘+cast(active_start_time as varchar(6)),6),3,2) active_start_time, substring(right(‘000‘+cast(active_end_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘000‘+cast(active_end_time as varchar(6)),6),3,2)+‘:‘ +substring(right(‘000‘+cast(active_end_time as varchar(6)),6),5,2) active_end_time, freq_subday_interval,‘Minutes‘ unit, ‘215‘ ServerName,‘Occurs every day every ‘+cast(freq_subday_interval as varchar(2))+‘ Minute(s) between ‘+ cast(active_start_time as varchar(10))+‘ and ‘+cast(active_end_time as varchar(10))+‘ .Schedule will be used starting on ‘+ cast(active_start_date as varchar(10)) DescriptionStr FROM [dbo].[sysjobs] JOB LEFT JOIN [dbo].[sysjobschedules] SysJobSche ON JOB.job_id = SysJobSche.job_id LEFT JOIN [dbo].[sysschedules] SysSche ON SysJobSche.schedule_id=SysSche.schedule_id WHERE JOB.enabled=1 and SysSche.name is not null --20 and SysSche.freq_type = 4 and SysSche.freq_subday_type = 4 --每天 几分钟循环一次 --------------freq_subday_type = 4----
---------------------------------By weekly-------------------------------------------- SELECT JOB.name,SysSche.name,--freq_interval, substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),3,2) active_start_time, case freq_interval when 1 then ‘Sunday‘ when 2 then ‘Monday‘ when 4 then ‘Tuesday‘ when 8 then ‘Wednesday‘ when 16 then ‘Thursday‘ when 32 then ‘Friday‘ when 64 then ‘Saturday‘ when 62 then ‘Monday,Tuesday,Wednesday,Thursday,Friday‘ when 63 then ‘Monday,Tuesday,Wednesday,Thursday,Friday,Sunday‘ when 18 then ‘Monday,Thursday‘ when 19 then ‘Sunday,Monday,Thursday‘ when 3 then ‘Sunday,Monday‘ end ExcuteDate, 215 server_name, ‘Occurs every week on ‘+ case freq_interval when 1 then ‘Sunday‘ when 2 then ‘Monday‘ when 4 then ‘Tuesday‘ when 8 then ‘Wednesday‘ when 16 then ‘Thursday‘ when 32 then ‘Friday‘ when 64 then ‘Saturday‘ when 62 then ‘Monday,Tuesday,Wednesday,Thursday,Friday‘ when 63 then ‘Monday,Tuesday,Wednesday,Thursday,Friday,Sunday‘ when 18 then ‘Monday,Thursday‘ when 19 then ‘Sunday,Monday,Thursday‘ when 3 then ‘Sunday,Monday‘ end +‘ at ‘+ substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),3,2) +‘. Schedule will be used starting on ‘ +cast(active_start_date as varchar(10)) DescriptionStr FROM [dbo].[sysjobs] JOB LEFT JOIN [dbo].[sysjobschedules] SysJobSche ON JOB.job_id = SysJobSche.job_id LEFT JOIN [dbo].[sysschedules] SysSche ON SysJobSche.schedule_id=SysSche.schedule_id WHERE JOB.enabled=1 and SysSche.name is not null --20 and SysSche.freq_type = 8 and SysSche.freq_subday_type = 1 --- order by SysSche.freq_subday_interval
---------------------------------By Month-------------------------------------------- SELECT JOB.name,SysSche.name,freq_interval, substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),3,2) active_start_time, 215 server_name, ‘Occurs every month on day ‘+cast(freq_interval as varchar(3))+‘ at ‘+ substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),1,2)+‘:‘ +substring(right(‘0000‘+cast(active_start_time as varchar(6)),6),3,2) +‘. Schedule will be used starting on ‘ +cast(active_start_date as varchar(10)) DescriptionStr FROM [dbo].[sysjobs] JOB LEFT JOIN [dbo].[sysjobschedules] SysJobSche ON JOB.job_id = SysJobSche.job_id LEFT JOIN [dbo].[sysschedules] SysSche ON SysJobSche.schedule_id=SysSche.schedule_id WHERE JOB.enabled=1 and SysSche.name is not null --20 and (SysSche.freq_type = 16 or SysSche.freq_type = 32) and SysSche.freq_subday_type = 1 --- order by SysSche.freq_subday_interval
不同的服务器需要修改serverName.