Oracle schedule on first day of month without holidays

4.3k views Asked by At

I need to create schedule for run jobs in oracle. It'll run at first workday of month, but excluding holidays. When first workday of month is holiday, than run should be next workday after holiday.

I managed with first workday of each month and exclude holidays, but I don't know how to set run on workday after holiday...

HOLIDAYS

DBMS_SCHEDULER.CREATE_SCHEDULE (
  schedule_name   => 'SCHDL_COM_HOLIDAYS',
  repeat_interval => 'FREQ=YEARLY; BYDATE=0101,0111;'
);

FIRST WORKDAY

DBMS_SCHEDULER.CREATE_SCHEDULE (
  schedule_name   => 'SCHDL_FIRST_WD',
  repeat_interval => 'FREQ=MONTHLY; BYDAY=MON,TUE,WED,THU,FRI; EXCLUDE=SCHDL_COM_HOLIDAYS; BYSETPOS=1'
);
2

There are 2 answers

2
XING On BEST ANSWER

Please follow the below steps:

1) Create schedule for all the holidays.

BEGIN
   DBMS_SCHEDULER.create_schedule (
      schedule_name     => 'NYD_FRI',
      repeat_interval   => 'FREQ=YEARLY;BYDATE=1231;BYDAY=FRI',
      comments          => 'Friday alternative for New Year''s Day');
   DBMS_SCHEDULER.create_schedule (
      schedule_name     => 'NYD_MON',
      repeat_interval   => 'FREQ=YEARLY;BYDATE=0102;BYDAY=MON',
      comments          => 'Monday alternative for New Year''s Day');
   DBMS_SCHEDULER.create_schedule (
      schedule_name     => 'NewYearsDay',
      repeat_interval   =>    'FREQ=YEARLY;BYDATE=0101;BYDAY=MON,TUE,WED,THU,FRI;'
                           || 'INCLUDE=NYD_FRI,NYD_MON',
      comments          => 'New Year''s Day');
  );
END;
/

2) Create a schedule which clubs all the schedule created above.

BEGIN
   DBMS_SCHEDULER.create_schedule (
      schedule_name     => 'FederalHolidays',
      repeat_interval   =>  'NewYearsDay,MartinLutherKing,PresidentsDay,',
      comments          => 'Federal Holidays');
END;

3) Create a job, note the “EXCLUDE” and “BYSETPOS” option

BEGIN
   DBMS_SCHEDULER.create_job (
      job_name          => 'Run_Next_Day',
      job_type          => 'PLSQL_BLOCK',
      job_action        => 'begin Run_Next_Day; end; ',
      repeat_interval   => 'FREQ=MONTHLY; BYDAY=MON,TUE,WED,THU,FRI; byhour=14;byminute=30;bysecond=0; EXCLUDE=FederalHolidays; BYSETPOS=7',
      enabled           => TRUE,
      comments          => 'Skipping Holidays');
END;
/
0
Gadziu On

@XING thank You for Your help. I modified your solution exactly to my needs.

The answer exactly for my problem is

New Year's Day

BEGIN
DBMS_SCHEDULER.create_schedule (
  schedule_name     => 'SCHD_NYD_MON',
  repeat_interval   => 'FREQ=YEARLY;BYDATE=0101;BYDAY=MON',
  comments          => 'Monday alternative for New Year''s Day');
DBMS_SCHEDULER.create_schedule (
  schedule_name     => 'SCHD_NEW_YEAR_DAY',
  repeat_interval   =>    'FREQ=YEARLY;BYDATE=0101;BYDAY=MON,TUE,WED,THU,FRI;'
                       || 'INCLUDE=SCHD_NYD_MON',
  comments          => 'New Year''s Day');
END;
/

First November's Day

BEGIN
DBMS_SCHEDULER.create_schedule (
  schedule_name     => 'SCHD_FNOV_MON',
  repeat_interval   => 'FREQ=YEARLY;BYDATE=1101;BYDAY=MON',
  comments          => 'Monday alternative for First November');
DBMS_SCHEDULER.create_schedule (
  schedule_name     => 'SCHD_FIRST_NOVEMBER',
  repeat_interval   =>    'FREQ=YEARLY;BYDATE=1101;BYDAY=MON,TUE,WED,THU,FRI;'
                       || 'INCLUDE=SCHD_FNOV_MON',
  comments          => 'First November''s Day');
END;
/

Federal Holidays

BEGIN
   DBMS_SCHEDULER.create_schedule (
      schedule_name     => 'SCHD_HOLIDAYS',
      repeat_interval   => 'SCHD_NEW_YEAR_DAY,SCHD_FIRST_NOVEMBER',
      comments          => 'Federal Holidays');
END;
/

Job

BEGIN
  DBMS_SCHEDULER.create_job (
     job_name          => 'Run_Next_Day',
     job_type          => 'PLSQL_BLOCK',
     job_action        => 'begin Run_Next_Day; end; ',
     repeat_interval   => 'FREQ=MONTHLY; BYDAY=MON,TUE,WED,THU,FRI; 
                           byhour=12;byminute=0;bysecond=0; 
                           EXCLUDE=SCHD_HOLIDAYS; BYSETPOS=1',
     enabled           => TRUE,
     comments          => 'Skipping Holidays');
END;
/