甲骨文 DBMS_SCHEDULE 至 运行 每周的每天上午 8 点、中午 12 点、下午 4 点和晚上 8 点

Oracle DBMS_SCHEDULE to run at 8am, 12pm, 4pm and 8pm every day of the week

我不确定如何使用 Oracle DBMS_SCHEDULER 解决问题。我被要求 运行 一个特定的 PL/SQL 程序工作在:

8AM, 12PM, 4PM and lastly at 8PM every day of the week 

然后在第二天的同一时间再次重新开始:8AM, 12PM, 4PM and lastly at 8PM

我知道我首先需要创建一个时间表,但不知道频率设置应该是多少,即:

dbms_scheduler.create_schedule( schedule_name   => 'MY_JOB',
                                repeat_interval => 'FREQ=DAILY;BYHOUR=8;BYMINUTE=00'
                                start_date      => SYSTIMESTAMP
                              );

您不必创建命名的 SCHEDULE(但如果您愿意,也可以这样做),您可以将重复间隔直接放在作业中:

DBMS_SCHEDULER.CREATE_JOB (
   job_name        => ...
  ,start_date      => SYSTIMESTAMP
  ,repeat_interval => 'FREQ=HOURLY;INTERVAL=1;BYHOUR=08,12,16,20;BYMINUTE=00'

请注意,对于频率低于每天的作业,您必须考虑夏令时。 SYSTIMESTAMP 的时区是数据库服务器操作系统的时区,通常设置为静态 UTC 偏移量(例如 +02:00)。为了将夏令时考虑在内,此 UTC 偏移量每年更改两次。

如果作业必须遵循夏令时调整,则您必须为 start_date 的时区指定区域名称。例如你可以做

DBMS_SCHEDULER.CREATE_JOB (
   job_name        => ...
  ,start_date      => SYSTIMESTAMP AT TIME ZONE 'Europe/Zurich'
  ,repeat_interval => 'FREQ=HOURLY;INTERVAL=1;BYHOUR=08,12,16,20;BYMINUTE=00'

看看这个 PL/SQL 块,看看有什么不同。

DECLARE
   next_run_date TIMESTAMP WITH TIME ZONE;
   start_date TIMESTAMP WITH TIME ZONE; 
BEGIN
   DBMS_OUTPUT.PUT_LINE('Static UTC offset:');
   start_date := TIMESTAMP '2019-10-26 00:00:00 +02:00';
   FOR i IN 1..10 LOOP
      DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING('FREQ=HOURLY;INTERVAL=1;BYHOUR=08,12,16,20;BYMINUTE=00', start_date, next_run_date, next_run_date);
      DBMS_OUTPUT.PUT_LINE(next_run_date AT LOCAL);
   END LOOP;

   DBMS_OUTPUT.PUT_LINE('Time zone region name:');
    next_run_date := NULL;
   start_date := TIMESTAMP '2019-10-26 00:00:00 Europe/Zurich';
   FOR i IN 1..10 LOOP
      DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING('FREQ=HOURLY;INTERVAL=1;BYHOUR=08,12,16,20;BYMINUTE=00', start_date, next_run_date, next_run_date);
      DBMS_OUTPUT.PUT_LINE(next_run_date AT LOCAL);
   END LOOP;

END;

Static UTC offset:
2019-10-26 08:00:00.000 +02:00
2019-10-26 12:00:00.000 +02:00
2019-10-26 16:00:00.000 +02:00
2019-10-26 20:00:00.000 +02:00
2019-10-27 07:00:00.000 +01:00
2019-10-27 11:00:00.000 +01:00
2019-10-27 15:00:00.000 +01:00
2019-10-27 19:00:00.000 +01:00
2019-10-28 07:00:00.000 +01:00
2019-10-28 11:00:00.000 +01:00

Time zone region name:
2019-10-26 08:00:00.000 +02:00
2019-10-26 12:00:00.000 +02:00
2019-10-26 16:00:00.000 +02:00
2019-10-26 20:00:00.000 +02:00
2019-10-27 08:00:00.000 +01:00
2019-10-27 12:00:00.000 +01:00
2019-10-27 16:00:00.000 +01:00
2019-10-27 20:00:00.000 +01:00
2019-10-28 08:00:00.000 +01:00
2019-10-28 12:00:00.000 +01:00

有关详细信息,请参阅 Repeat Intervals and Daylight Savings