Tuesday, 13 April 2021

Scheduling Jobs with DBMS_SCHEDULER in Oracle (PL/SQL Procedure)

 First define a program that is capable of executing PL/SQL script:

CREATE PROCEDURE xx_plsql_script_01
IS
  . . . .
EXCEPTION
WHEN OTHERS THEN
  -------Error Message
END;


STEP 1 – Create program

BEGIN
  dbms_scheduler.create_program( program_name => 'stored_procedure_program',
                                program_type => 'STORED_PROCEDURE',
                                program_action => 'XX_PLSQL_SCRIPT_01',
                                enabled => FALSE,
                                comments => 'Program for Data Insert/Update');
  dbms_scheduler.ENABLE (name=>'stored_procedure_program');
END;

STEP 2 – Create Schedule

BEGIN
  dbms_scheduler.create_schedule ( schedule_name => 'XX_PLSQL_SCRIPT_01_11AM_SCHEDULE',
                                  start_date => systimestamp,
                                  repeat_interval => 'freq=daily; byhour=11; byminute=0; bysecond=0;',
                                  end_date => NULL,
                                  comments => 'Run 11AM everyday');
END;

STEP 3 – Create Job

BEGIN
  dbms_scheduler.create_job ( job_name => 'XX_PLSQL_SCRIPT_01_11am_JOB',
                             program_name => 'stored_procedure_program',
                             schedule_name => 'XX_PLSQL_SCRIPT_01_11AM_SCHEDULE',
                             enabled => TRUE,
                             comments => 'My test scheduler job for 11am');
END;


Ref: https://www.support.dbagenesis.com/post/scheduling-jobs-with-dbms_scheduler

No comments:

Post a Comment