首页 > 代码库 > 获取指定时间区间作业运行情况

获取指定时间区间作业运行情况

背景:数据库服务器定期重启,想知道重启期间对作业的影响。通俗点就是服务器在重启这段时间,有哪些作业计划要运行,重启后是否要手动执行这些作业?
第一次重启的时候,按照最笨的方式,把所有作业看一遍,然后人为判断有哪些作业将受到影响,再根据作业具体代码,确定是否需手动执行。后来老大说要弄个过程出来,通过传入起止时间参数,返回区间内的作业计划。
PS:参考各类资料,修改过很多遍,最后成型在6月初,很多细节上的修改自己也记不清楚了,一直懒得整理。先放上代码,以及效果图。

  1 /************************************************************  2  * 说明:输入将来的起、止时间,得到时间区间内作业计划情况。主要包括:  3          区间内首次执行时间、区间内末次执行时间、区间内执行次数、循环间隔、每天执行频率等。  4  * 使用:DBA_Pro_GetJobSchedules ‘20140612 07:00:00.000‘,‘20140612 08:00:00.000‘  5  * 补充:手动运行作业,不会影响作业的计划调度。  6  ************************************************************/  7 CREATE PROC DBA_Pro_GetJobSchedules  8 @Starttime DATETIME,  9 @Endtime DATETIME 10 AS 11  12 DECLARE @Midtime DATETIME 13 IF (@Starttime < GETDATE() OR @Starttime >= @Endtime) 14 BEGIN 15     PRINT(请输入将来的起、止时间,且开始时间小于结束时间!) 16     RETURN 17 END 18  19 CREATE TABLE #RunningJobs 20 ( 21     name                        VARCHAR(128)--作业名称 22    ,schedule_id                 INT--调度id 23    ,next_scheduled_run_date     DATETIME--下次运行时间 yyyy-mm-dd hh:mi:ss:mmm 24    ,active_start_time           DATETIME--执行间隔:开始时间 yyyy-mm-dd hh:mi:ss:mmm 25    ,active_end_time             DATETIME--执行间隔:结束时间 yyyy-mm-dd hh:mi:ss:mmm 26    ,starttime                   DATETIME--运行区间开始时间,比较传入的@Starttime和active_start_time,取大者 27    ,endtime                     DATETIME--运行区间结束时间,比较传入的@Endtime和active_end_time,取小者 28    ,daystatus                   INT--日期状态,用于标记当天是否满足作业运行日期,为0时当天将运行 29    ,timestaus                   INT--时间状态,用于标记当天只执行一次的作业是否已运行,为0时当天将运行 30 ) 31  32 WHILE (@Starttime < @Endtime) 33 BEGIN 34     SELECT @Midtime = CASE  35                            WHEN DATEADD(DAY ,1 ,@Starttime) > @Endtime THEN @Endtime 36                            ELSE CONVERT(VARCHAR ,DATEADD(DAY ,1 ,@starttime) ,112) 37                       END 38      39     INSERT INTO #RunningJobs 40     SELECT sj.name 41           ,scd.schedule_id 42           ,c.next_scheduled_run_date 43           ,CONVERT( 44                DATETIME 45               ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 46                STUFF( 47                    STUFF(RIGHT(000000 + CAST([active_start_time] AS VARCHAR(6)) ,6),3,0,:) 48                   ,6,0,: 49                ) 50            ) [active_start_time] 51           ,CONVERT( 52                DATETIME 53               ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 54                STUFF( 55                    STUFF(RIGHT(000000 + CAST([active_end_time] AS VARCHAR(6)) ,6),3,0,:) 56                   ,6,0,: 57                ) 58            ) [active_end_time] 59           ,CASE  60                 WHEN @Starttime < 61                      CONVERT( 62                          DATETIME 63                         ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 64                          STUFF( 65                              STUFF(RIGHT(000000 + CAST([active_start_time] AS VARCHAR(6)) ,6),3,0,:) 66                             ,6,0,: 67                          ) 68                      ) THEN CONVERT( 69                          DATETIME 70                         ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 71                          STUFF( 72                              STUFF(RIGHT(000000 + CAST([active_start_time] AS VARCHAR(6)) ,6),3,0,:) 73                             ,6,0,: 74                          ) 75                      ) 76                 ELSE @Starttime 77            END     starttime 78           ,CASE  79                 WHEN @Midtime > 80                      CONVERT( 81                          DATETIME 82                         ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 83                          STUFF( 84                              STUFF(RIGHT(000000 + CAST([active_end_time] AS VARCHAR(6)) ,6),3,0,:) 85                             ,6,0,: 86                          ) 87                      ) THEN CONVERT( 88                          DATETIME 89                         ,CONVERT(VARCHAR(10) ,@Starttime ,120) +   + 90                          STUFF( 91                              STUFF(RIGHT(000000 + CAST([active_end_time] AS VARCHAR(6)) ,6),3,0,:) 92                             ,6,0,: 93                          ) 94                      ) 95                 ELSE @Midtime 96            END     endtime 97           ,CASE freq_type 98                 WHEN 1 --OneTime 99                       THEN DATEDIFF(day ,next_scheduled_run_date ,@Starttime)100                 WHEN 4 --Daily101                       THEN DATEDIFF(dd ,next_scheduled_run_date ,@Starttime)%freq_interval102                 WHEN 8 --Weekly103                       THEN DATEDIFF(week ,next_scheduled_run_date ,@Starttime)%freq_recurrence_factor104                      +105                      CASE 106                           WHEN freq_interval & 1 = POWER(2 ,DATEPART(dw ,@Starttime) -1)107                                OR freq_interval & 2 = POWER(2 ,DATEPART(dw ,@Starttime) -1)108                                OR freq_interval & 4 = POWER(2 ,DATEPART(dw ,@Starttime) -1)109                                OR freq_interval & 8 = POWER(2 ,DATEPART(dw ,@Starttime) -1)110                                OR freq_interval & 16 = POWER(2 ,DATEPART(dw ,@Starttime) -1)111                                OR freq_interval & 32 = POWER(2 ,DATEPART(dw ,@Starttime) -1)112                                OR freq_interval & 64 = POWER(2 ,DATEPART(dw ,@Starttime) -1) THEN 0113                      END114                 WHEN 16 --Monthly115                       THEN DATEDIFF(MONTH ,next_scheduled_run_date ,@Starttime)%freq_recurrence_factor116                      +117                      CASE 118                           WHEN freq_interval = DAY(@Starttime) THEN 0119                      END120                 WHEN 32 --Monthly - Relative to Frequency Interval121                       THEN DATEDIFF(MONTH ,next_scheduled_run_date ,@Starttime)%freq_recurrence_factor122                      +123                      CASE 124                           WHEN freq_interval = DATEPART(dw ,@Starttime) --month of week1-7125                                AND 126                                (127                                    (128                                        freq_relative_interval <= 8129                                        AND freq_relative_interval = POWER(130                                                2131                                               ,DATEDIFF(DAY ,CONVERT(CHAR(6) ,@Starttime ,112) + 01 ,@Starttime) / 7132                                            )133                                    )134                                    OR (135                                           freq_relative_interval = 16136                                           AND DATEDIFF(137                                                   DAY138                                                  ,@Starttime139                                                  ,DATEADD(140                                                       DAY141                                                      ,-1142                                                      ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)143                                                   )144                                               ) < 7145                                       )146                                ) THEN 0147                           WHEN freq_interval = 8 --month of day148                                AND149                                (150                                    (151                                        freq_relative_interval = 1152                                        AND DATEDIFF(DAY ,CONVERT(CHAR(6) ,@Starttime ,112) + 01 ,@Starttime) = 0153                                    )154                                    OR (155                                           freq_relative_interval = 2156                                           AND DATEDIFF(DAY ,CONVERT(CHAR(6) ,@Starttime ,112) + 02 ,@Starttime) = 0157                                       )158                                    OR (159                                           freq_relative_interval = 4160                                           AND DATEDIFF(DAY ,CONVERT(CHAR(6) ,@Starttime ,112) + 03 ,@Starttime) = 0161                                       )162                                    OR (163                                           freq_relative_interval = 8164                                           AND DATEDIFF(DAY ,CONVERT(CHAR(6) ,@Starttime ,112) + 04 ,@Starttime) = 0165                                       )166                                    OR (167                                           freq_relative_interval = 16168                                           AND CONVERT(VARCHAR ,@Starttime ,112) = 169                                               CONVERT(170                                                   VARCHAR171                                                  ,DATEADD(172                                                       DAY173                                                      ,-1174                                                      ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)175                                                   )176                                                  ,112177                                               )178                                       )179                                ) THEN 0180                           WHEN freq_interval = 9 --month of weekday181                                AND182                                (183                                    (184                                        freq_relative_interval = 1185                                        AND DATEADD(186                                                DAY187                                               ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)188                                                     WHEN 1 THEN 1189                                                     WHEN 7 THEN 2190                                                     ELSE 0191                                                END192                                               ,CONVERT(CHAR(6) ,@Starttime ,112) + 01193                                            ) = CONVERT(VARCHAR ,@Starttime ,112)194                                    )195                                    OR (196                                           freq_relative_interval = 2197                                           AND DATEADD(198                                                   DAY199                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)200                                                        WHEN 6 THEN 3201                                                        WHEN 7 THEN 3202                                                        WHEN 1 THEN 2203                                                        ELSE 1204                                                   END205                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01206                                               ) = CONVERT(VARCHAR ,@Starttime ,112)207                                       )208                                    OR (209                                           freq_relative_interval = 4210                                           AND DATEADD(211                                                   DAY212                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)213                                                        WHEN 5 THEN 4214                                                        WHEN 6 THEN 4215                                                        WHEN 7 THEN 4216                                                        WHEN 1 THEN 3217                                                        ELSE 2218                                                   END219                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01220                                               ) = CONVERT(VARCHAR ,@Starttime ,112)221                                       )222                                    OR (223                                           freq_relative_interval = 8224                                           AND DATEADD(225                                                   DAY226                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)227                                                        WHEN 4 THEN 5228                                                        WHEN 5 THEN 5229                                                        WHEN 6 THEN 5230                                                        WHEN 7 THEN 5231                                                        WHEN 1 THEN 4232                                                        ELSE 3233                                                   END234                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01235                                               ) = CONVERT(VARCHAR ,@Starttime ,112)236                                       )237                                    OR (238                                           freq_relative_interval = 16239                                           AND DATEADD(240                                                   DAY241                                                  ,CASE DATEPART(242                                                            dw243                                                           ,DATEADD(244                                                                DAY245                                                               ,-1246                                                               ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)247                                                            )248                                                        )249                                                        WHEN 1 THEN -2250                                                        WHEN 7 THEN -1251                                                        ELSE 0252                                                   END253                                                  ,DATEADD(254                                                       DAY255                                                      ,-1256                                                      ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)257                                                   )258                                               ) = CONVERT(VARCHAR ,@Starttime ,112)259                                       )260                                ) THEN 0261                           WHEN freq_interval = 10 --month of weekend262                                AND263                                (264                                    (265                                        freq_relative_interval = 1266                                        AND DATEADD(267                                                DAY268                                               ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)269                                                     WHEN 2 THEN 5270                                                     WHEN 3 THEN 4271                                                     WHEN 4 THEN 3272                                                     WHEN 5 THEN 2273                                                     WHEN 6 THEN 1274                                                     ELSE 0275                                                END276                                               ,CONVERT(CHAR(6) ,@Starttime ,112) + 01277                                            ) = CONVERT(VARCHAR ,@Starttime ,112)278                                    )279                                    OR (280                                           freq_relative_interval = 2281                                           AND DATEADD(282                                                   DAY283                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)284                                                        WHEN 3 THEN 5285                                                        WHEN 4 THEN 4286                                                        WHEN 5 THEN 3287                                                        WHEN 6 THEN 2288                                             WHEN 7 THEN 1289                                                        ELSE 6290                                                   END291                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01292                                               ) = CONVERT(VARCHAR ,@Starttime ,112)293                                       )294                                    OR (295                                           freq_relative_interval = 4296                                           AND DATEADD(297                                                   DAY298                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)299                                                        WHEN 2 THEN 12300                                                        WHEN 3 THEN 11301                                                        WHEN 4 THEN 10302                                                        WHEN 5 THEN 9303                                                        WHEN 6 THEN 8304                                                        ELSE 7305                                                   END306                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01307                                               ) = CONVERT(VARCHAR ,@Starttime ,112)308                                       )309                                    OR (310                                           freq_relative_interval = 8311                                           AND DATEADD(312                                                   DAY313                                                  ,CASE DATEPART(dw ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)314                                                        WHEN 3 THEN 12315                                                        WHEN 4 THEN 11316                                                        WHEN 5 THEN 10317                                                        WHEN 6 THEN 9318                                                        WHEN 7 THEN 8319                                                        ELSE 13320                                                   END321                                                  ,CONVERT(CHAR(6) ,@Starttime ,112) + 01322                                               ) = CONVERT(VARCHAR ,@Starttime ,112)323                                       )324                                    OR (325                                           freq_relative_interval = 16326                                           AND DATEADD(327                                                   DAY328                                                  ,CASE DATEPART(329                                                            dw330                                                           ,DATEADD(331                                                                DAY332                                                               ,-1333                                                               ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)334                                                            )335                                                        )336                                                        WHEN 2 THEN -1337                                                        WHEN 3 THEN -2338                                                        WHEN 4 THEN -3339                                                        WHEN 5 THEN -4340                                                        WHEN 6 THEN -5341                                                        ELSE 0342                                                   END343                                                  ,DATEADD(344                                                       DAY345                                                      ,-1346                                                      ,DATEADD(MONTH ,1 ,CONVERT(CHAR(6) ,@Starttime ,112) + 01)347                                                   )348                                               ) = CONVERT(VARCHAR ,@Starttime ,112)349                                       )350                                ) THEN 0351                      END352            END  AS daystatus353           ,CASE 354                 WHEN freq_subday_type IN (0 ,1) 355                      THEN 356                      CASE 357                           WHEN active_start_time*1000358                                >= CONVERT(INT ,REPLACE(CONVERT(VARCHAR ,@Starttime ,114) ,: ,‘‘)) 359                                THEN 0360                           ELSE 1361                      END362                 ELSE 0363            END AS timestaus364     FROM   [msdb].[dbo].[sysschedules] scd365            INNER JOIN msdb.dbo.sysjobschedules sjsc366                 ON  scd.schedule_id = sjsc.schedule_id367            INNER JOIN (368                     SELECT a.job_id369                           ,a.next_scheduled_run_date370                     FROM   msdb.dbo.sysjobactivity a371                            INNER JOIN (372                                     SELECT MAX(session_id) session_id373                                     FROM   msdb.dbo.syssessions374                                 ) b375                                 ON  a.session_id = b.session_id376                 ) c377                 ON  sjsc.job_id = c.job_id378            INNER JOIN msdb.dbo.sysjobs sj379                 ON  c.job_id = sj.job_id380     WHERE  scd.enabled = 1381            AND sj.enabled = 1382            --AND c.next_scheduled_run_date <= @Midtime383     384     SET @Starttime = CONVERT(VARCHAR ,DATEADD(DAY ,1 ,@starttime) ,112)385 END386 --select * from #RunningJobs order by name,active_start_time387 SELECT name--作业名称388       ,plan_type--计划类型389       ,frequency--执行频率每天、每周、每月390       ,next_scheduled_run_date--下次计划运行时间391       ,FirstRunTimeInRange--区间内首次运行时间392       ,LastRunTimeInRange--区间内末次运行时间393       ,RunTimesInRange--区间内运行次数394       ,intercycle--循环间隔395       ,execution_interval--每天执行频率及区间396       ,active_start_date--开始日期397       ,active_end_date--结束日期398       ,date_created--创建时间399       ,date_modified--修改时间400 FROM   (401            SELECT a.name402                  ,a.schedule_id403                  ,a.active_start_time404                  ,a.starttime405                  ,a.endtime406                  ,freq_type,freq_subday_type407                  ,freq_subday_interval408                  ,CASE 409                        WHEN [freq_type] = 64 THEN 410                             Start automatically when SQL Server Agent starts411                        WHEN [freq_type] = 128 THEN 412                             Start whenever the CPUs become idle413                        WHEN [freq_type] IN (4 ,8 ,16 ,32) THEN Recurring414                        WHEN [freq_type] = 1 THEN One Time415                   END                    AS plan_type416                  ,CASE [freq_type]417                        WHEN 1 THEN One Time418                        WHEN 4 THEN Daily419                        WHEN 8 THEN Weekly420                        WHEN 16 THEN Monthly421                        WHEN 32 THEN Monthly - Relative to Frequency Interval422                        WHEN 64 THEN Start automatically when SQL Server Agent starts423                        WHEN 128 THEN Start whenever the CPUs become idle424                   END                    AS frequency425                  ,a.next_scheduled_run_date426                  ,CASE [freq_subday_type]427                        WHEN 2 THEN DATEADD(428                                 second429                                ,(430                                     freq_subday_interval -431                                     DATEDIFF(second ,a.active_start_time ,a.starttime)432                                     %freq_subday_interval433                                 )%freq_subday_interval434                                ,a.starttime435                             )436                        WHEN 4 THEN DATEADD(437                                 second438                                ,(439                               freq_subday_interval * 60 -440                                     DATEDIFF(second ,a.active_start_time ,a.starttime)441                                     %(freq_subday_interval * 60)442                                 )%(freq_subday_interval * 60)443                                ,a.starttime444                             )445                        WHEN 8 THEN DATEADD(446                                 second447                                ,(448                                     freq_subday_interval * 3600 -449                                     DATEDIFF(second ,a.active_start_time ,a.starttime)450                                     %(freq_subday_interval * 3600)451                                 )%(freq_subday_interval * 3600)452                                ,a.starttime453                             )454                        ELSE a.active_start_time455                   END                    AS FirstRunTimeInRange456                  ,CASE [freq_subday_type]457                        WHEN 2 THEN DATEADD(458                                 second459                                ,-DATEDIFF(second ,a.active_start_time ,a.endtime)460                                 %freq_subday_interval461                                ,a.endtime462                             )463                        WHEN 4 THEN DATEADD(464                                 second465                                ,-DATEDIFF(second ,a.active_start_time ,a.endtime)466                                 %(freq_subday_interval * 60)467                                ,a.endtime468                             )469                        WHEN 8 THEN DATEADD(470                                 second471                                ,-DATEDIFF(second ,a.active_start_time ,a.endtime)472                                 %(freq_subday_interval * 3600)473                                ,a.endtime474                             )475                        ELSE a.active_start_time476                   END                    AS LastRunTimeInRange477                  ,CASE [freq_subday_type]478                        WHEN 2 THEN DATEDIFF(479                                 second480                                ,DATEADD(481                                     second482                                    ,(483                                         freq_subday_interval -484                                         DATEDIFF(second ,a.active_start_time ,a.starttime)485                                         %freq_subday_interval486                                     )%freq_subday_interval487                                    ,a.starttime488                                 )489                                ,a.endtime490                             ) / (freq_subday_interval) + 1491                        WHEN 4 THEN DATEDIFF(492                                 second493                                ,DATEADD(494                                     second495                                    ,(496                                         freq_subday_interval * 60 -497                                         DATEDIFF(second ,a.active_start_time ,a.starttime)498                                         %(freq_subday_interval * 60)499                                     )%(freq_subday_interval * 60)500                                    ,a.starttime501                                 )502                                ,a.endtime503                             ) / (freq_subday_interval * 60) + 1504                        WHEN 8 THEN DATEDIFF(505                                 second506                                ,DATEADD(507                                     second508                                    ,(509                                         freq_subday_interval * 3600 -510                                         DATEDIFF(second ,a.active_start_time ,a.starttime)511                                         %(freq_subday_interval * 3600)512                                     )%(freq_subday_interval * 3600)513                                    ,a.starttime514                               )515                                ,a.endtime516                             ) / (freq_subday_interval * 3600) + 1517                        ELSE 1518                   END                    AS RunTimesInRange519                  ,CASE [freq_type]520                        WHEN 4 THEN Occurs every  + CAST([freq_interval] AS VARCHAR(3))521                             +  day(s)522                        WHEN 8 THEN Occurs every  + CAST([freq_recurrence_factor] AS VARCHAR(3))523                             +  week(s) on 524                             + CASE 525                                    WHEN [freq_interval] & 1 = 1 THEN Sunday526                                    ELSE ‘‘527                               END 528                             + CASE 529                                    WHEN [freq_interval] & 2 = 2 THEN , Monday530                                    ELSE ‘‘531                               END532                             + CASE 533                                    WHEN [freq_interval] & 4 = 4 THEN , Tuesday534                                    ELSE ‘‘535                               END 536                             + CASE 537                                    WHEN [freq_interval] & 8 = 8 THEN , Wednesday538                                    ELSE ‘‘539                               END540                             + CASE 541                                    WHEN [freq_interval] & 16 = 16 THEN , Thursday542                                    ELSE ‘‘543                               END 544                             + CASE 545                                    WHEN [freq_interval] & 32 = 32 THEN , Friday546                                    ELSE ‘‘547                               END548                             + CASE 549                                    WHEN [freq_interval] & 64 = 64 THEN , Saturday550                                    ELSE ‘‘551                               END552                        WHEN 16 THEN Occurs on Day  + CAST([freq_interval] AS VARCHAR(3))553                             +  of every  + CAST([freq_recurrence_factor] AS VARCHAR(3))554                             +  month(s)555                        WHEN 32 THEN Occurs on  + CASE [freq_relative_interval]556                                                         WHEN 1 THEN First557                                                         WHEN 2 THEN Second558                                                         WHEN 4 THEN Third559                                                         WHEN 8 THEN Fourth560                                                         WHEN 16 THEN Last561                                                    END +   + CASE 562                                                                     [freq_interval]563                                                                     WHEN 1 THEN 564                                                                          Sunday565                                                                     WHEN 2 THEN 566                                                                          Monday567                                                                     WHEN 3 THEN 568                                                                          Tuesday569                                                                     WHEN 4 THEN 570                                                                          Wednesday571                                                                     WHEN 5 THEN 572                                                                          Thursday573                                                                     WHEN 6 THEN 574                                                                          Friday575                                                                     WHEN 7 THEN 576                                                                          Saturday577                                                                     WHEN 8 THEN 578                                                                          Day579                                                                     WHEN 9 THEN 580                                                                          Weekday581                                                                     WHEN 10 THEN 582                                                                          Weekend day583                                                                END +584                              of every 585                             + CAST([freq_recurrence_factor] AS VARCHAR(3)) +586                              month(s)587                   END                    AS intercycle588                  ,CASE [freq_subday_type]589                        WHEN 1 THEN Occurs once at  + CONVERT(VARCHAR ,a.active_start_time ,108)590                        WHEN 2 THEN Occurs every  + CAST([freq_subday_interval] AS VARCHAR(3))591                             +  Second(s) between  + CONVERT(VARCHAR ,a.active_start_time ,108)592                             +  and  + CONVERT(VARCHAR ,a.active_end_time ,108)593                        WHEN 4 THEN Occurs every  + CAST([freq_subday_interval] AS VARCHAR(3)) 594                             +  Minute(s) between  + CONVERT(VARCHAR ,a.active_start_time ,108)595                             +  and  + CONVERT(VARCHAR ,a.active_end_time ,108)596                        WHEN 8 THEN Occurs every  + CAST([freq_subday_interval] AS VARCHAR(3))597                             +  Hour(s) between  + CONVERT(VARCHAR ,a.active_start_time ,108)598                             +  and  + CONVERT(VARCHAR ,a.active_end_time ,108)599                   END                    AS execution_interval600                  ,STUFF(601                       STUFF(CAST([active_start_date] AS VARCHAR(8)) ,5 ,0 ,-)602                      ,8,0,-603                   )                      AS active_start_date604                  ,STUFF(605                       STUFF(CAST([active_end_date] AS VARCHAR(8)) ,5 ,0 ,-)606                      ,8,0,-607                   )                      AS active_end_date608                  ,scd.[date_created] AS date_created609                  ,scd.[date_modified] AS date_modified610            FROM   #RunningJobs a611                   INNER JOIN [msdb].[dbo].[sysschedules] scd612                        ON  a.schedule_id = scd.schedule_id613            WHERE  a.daystatus = 0614            AND a.timestaus=0615        ) a616 WHERE  a.FirstRunTimeInRange <= a.endtime617 AND a.FirstRunTimeInRange <= CONVERT(DATETIME ,a.active_end_date +  23:59:59:997)618 ORDER BY619        a.name620       ,a.FirstRunTimeInRange621 622 DROP TABLE #RunningJobs
View Code

执行作业将返回区间内首次执行时间、区间内末次执行时间、区间内执行次数、循环间隔、每天执行频率等信息