如何在dbms_scheduler中安排一个作业每两周的星期日运行。

huangapple go评论103阅读模式
英文:

How to schedule a job to run every other Sunday of a week in dbms_scheduler

问题

Subsequent Execution Expression: NEXT_DAY(TRUNC(SYSDATE), 'SUNDAY', 1 + (2 * (TRUNC(TO_NUMBER(TO_CHAR(SYSDATE, 'J')) / 7) / 2)))

英文:

If I want to run a job every Sunday at Midnight then the expression for subsequent executions would be like:

Subsequent Execution Expression: NEXT_DAY(TRUNC(SYSDATE), 'SUNDAY')

job query will look like following:

 SYS.DBMS_JOB.SUBMIT
    ( job       => NewJobID 
     ,what      => 'X_Process'
     ,next_date => TO_DATE('8/2/2023 11:45:00 AM', 'MM/DD/YYYY HH:MI:SS AM')
     ,interval  => 'NEXT_DAY(TRUNC(SYSDATE), 'SUNDAY')'
     ,no_parse  => FALSE);
  SYS.DBMS_OUTPUT.PUT_LINE
    ('Job Number is: ' || to_char(NewJobID));
  COMMIT;

But I need to execute the job every other Sunday means the job will run One week on Sunday then the following week's Sunday it will not run. But after that, it will run again next week's Sunday.
So execution will be twice in a four-week span.

So what will be the Expression for this interval?

答案1

得分: 1

以下是关于 dbms_scheduler 的几个选项(这是您在标题中指定的内容,而您发布的代码包含 dbms_job。切换到 dbms_scheduler,它更强大)。

选项一:每隔一周运行作业“weekly”(因此每隔一周)在“SUN”天的“11”小时“45”分钟:

BEGIN
   DBMS_SCHEDULER.CREATE_JOB (
      job_name         =>'My_job',
      job_type         =>'PLSQL_BLOCK',
      job_action       =>'begin x_process; end;',
      start_date       =>
         TO_TIMESTAMP_TZ ('06.08.2023 11:45 Europe/Zagreb', 'dd.mm.yyyy hh24:mi TZR'),
      repeat_interval  =>'FREQ=WEEKLY; INTERVAL=2; BYDAY=SUN; BYHOUR=11; BYMINUTE=45',
      enabled          => TRUE,
      comments         =>'Job runs every other Sunday');
END;
/

选项二:每月运行作业“monthly”在“第1、第3和第5个星期日”的“11:45”;如果月份只有4个星期日,则第5个星期日不会执行:

repeat_interval  =>'FREQ=MONTHLY; BYDAY=1SUN,3SUN,5SUN; BYHOUR=11; BYMINUTE=45',

选项三:每隔一年运行作业“yearly”(您必须逐个指定从1到53的所有周数(我放置了“...”因为我不想一个一个地输入)在“星期日”的“11:45”:

repeat_interval  =>'FREQ=YEARLY; BYWEEKNO=1,3,5,7,9,11,...,49,51,53; BYDAY=SUN; BYHOUR=11; BYMINUTE=45',

还可能有其他选项;希望其他人能提出建议。

此外,请查看 DBMS_SCHEDULER 文档 以获取更多信息。

至于 DBMS_JOBDBMS_SCHEDULER:您最好不再使用 DBMS_JOBOracle文档(19c版本;最新版本是23免费)指出:

Oracle Scheduler replaces the DBMS_JOB package. Although DBMS_JOB is still supported for backward compatibility, Oracle strongly recommends that you switch from DBMS_JOB to Oracle Scheduler.

因此,可以预期它将被弃用/不再支持。您应该切换到 DBMS_SCHEDULER

英文:

Here are a few options related to dbms_scheduler (which is what you specified in title, while code you posted contains dbms_job. Switch to dbms_scheduler, it is more powerful).

Option one: run job "weekly" with interval of "2" (so, every other week) on "SUN"day at "11" hours "45" minutes:

BEGIN
   DBMS_SCHEDULER.CREATE_JOB (
      job_name         => 'My_job',
      job_type         => 'PLSQL_BLOCK',
      job_action       => 'begin x_process; end;',
      start_date       =>
         TO_TIMESTAMP_TZ ('06.08.2023 11:45 Europe/Zagreb', 'dd.mm.yyyy hh24:mi TZR'),
      repeat_interval  => 'FREQ=WEEKLY; INTERVAL=2; BYDAY=SUN; BYHOUR=11; BYMINUTE=45',
      enabled          => TRUE,
      comments         => 'Job runs every other Sunday');
END;
/

Option two: run job "monthly" on "1st, 3rd and 5th Sunday" at "11:45"; if there are only 4 Sundays in month, the 5th won't be executed):

repeat_interval  => 'FREQ=MONTHLY; BYDAY=1SUN,3SUN,5SUN; BYHOUR=11; BYMINUTE=45',

Option three: run job "yearly", every other week (you have to specify all week numbers from 1 to 53, one-by-one (I put "..." because I didn't fell like typing all of them) on "Sunday" at "11:45":

repeat_interval  => 'FREQ=YEARLY; BYWEEKNO=1,3,5,7,9,11,...,49,51,53; BYDAY=SUN; BYHOUR=11; BYMINUTE=45',

There might be other options; hopefully, someone else will suggest them.

Also, have a look at <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SCHEDULER.html#GUID-73622B78-EFF4-4D06-92F5-E358AB2D58F3">DBMS_SCHEDULER</A> documentation for more information.


As of DBMS_JOB vs. DBMS_SCHEDULER: you'd rather not use DBMS_JOB any longer. <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/support-for-DBMS_JOB.html#GUID-CC42E8DA-7203-449E-BCEA-8187ECC4BCDF">Support for DBMS_JOB</a> (19c version; the newest is 23 free) Oracle documentation says:

> Oracle Scheduler replaces the DBMS_JOB package. Although DBMS_JOB is still supported for backward compatibility, Oracle strongly recommends that you switch from DBMS_JOB to Oracle Scheduler.

so it is expected that it'll become deprecated / desupported. You should switch to DBMS_SCHEDULER.

huangapple
  • 本文由 发表于 2023年7月17日 12:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定