Sunday, April 14, 2019
Example Script to create Database Jobs (DBMS_SCHEDULER.CREATE_JOB)
In this post, we have a given
an example script to schedule a database job to run a PLSQL procedure for every
2 minutes once.
Wrapper Procedure:
CREATE OR REPLACE PROCEDURE schedule_job(pov_errbuf OUT VARCHAR2,pon_retcode OUT NUMBER,piv_job_name IN VARCHAR2,piv_comments IN VARCHAR2,pid_start_date IN VARCHAR2,piv_repeat_frequency IN VARCHAR2,piv_repeat_interval IN VARCHAR2,pid_end_date IN VARCHAR2,piv_program_name IN VARCHAR2,piv_enabled IN VARCHAR2 DEFAULT 'Y',piv_auto_drop IN VARCHAR2 DEFAULT 'N')ISle_end_of_program EXCEPTION;lb_enabled BOOLEAN;lb_auto_drop BOOLEAN DEFAULT FALSE;lv_repeat_interval VARCHAR2(200);lv_dummy_char VARCHAR2(200);lrec_job all_scheduler_jobs%ROWTYPE;position_ NUMBER;BEGINposition_ := 10;IF piv_enabled = 'Y'THENlb_enabled := TRUE;END IF;position_ := 20;IF piv_auto_drop = 'Y'THENlb_auto_drop := TRUE;END IF;position_ := 30;-- prepare the repeat intervallv_repeat_interval := 'FREQ='||piv_repeat_frequency||';INTERVAL='||piv_repeat_interval;position_ := 40;-- check is job already existsBEGINSELECT job_nameINTO lv_dummy_charFROM dba_scheduler_jobsWHERE UPPER(job_name) = piv_job_name;pov_errbuf := 'Job already exists.';RAISE le_end_of_program;EXCEPTIONWHEN NO_DATA_FOUND THENNULL;WHEN OTHERS THENpov_errbuf := 'Error while validating Job.Error:'||SQLERRM;RAISE le_end_of_program;END;position_ := 50;-- check is any database object already exists with same nameBEGINSELECT object_typeINTO lv_dummy_charFROM dba_objectsWHERE UPPER(object_name) = piv_job_name;pov_errbuf := 'A Database Object of type '||lv_dummy_char||' already exists.Error:'||SQLERRM;RAISE le_end_of_program;EXCEPTIONWHEN NO_DATA_FOUND THENNULL;WHEN OTHERS THENpov_errbuf := 'Error while validating Job.Error:'||SQLERRM;RAISE le_end_of_program;END;position_ := 60;-- Job defined by an existing program and inline schedule.DBMS_SCHEDULER.create_job (job_name => piv_job_name,job_type => 'PLSQL_BLOCK',job_action => 'BEGIN '||REPLACE(piv_program_name,'''','''''')||'; END;',start_date => TO_DATE(pid_start_date,'RRRR/MM/DD HH24:MI:SS'),repeat_interval => lv_repeat_interval,end_date => TO_DATE(pid_end_date,'RRRR/MM/DD HH24:MI:SS'),enabled => lb_enabled,comments => piv_comments);EXCEPTIONWHEN le_end_of_program THENpon_retcode := 1;DBMS_OUTPUT.PUT_LINE(pov_errbuf);WHEN OTHERS THENpov_errbuf := 'Unhandled Error while Creating job at position '||position_||'Error:'||SQLERRM;pon_retcode := 2;END schedule_job;
Sample Package:
CREATE OR REPLACE PROCEDURE TEST_MAINISBEGINNULL;END;
Testing Script:
SET SERVEROUTPUT ON;DECLARElv_errbuf VARCHAR2(4000);ln_retcode NUMBER;BEGINxxif_manage_schedules_pkg.schedule_job(pov_errbuf => lv_errbuf,pon_retcode => ln_retcode,piv_job_name => 'TEST_SHAREORACLEAPPS',piv_comments => 'Job defined inline schedule.',pid_start_date => TO_CHAR(SYSDATE,'RRRR/MM/DD HH24:MI:SS'),piv_repeat_frequency => 'MINUTELY',piv_repeat_interval => '2',pid_end_date => TO_CHAR(SYSDATE+1,'RRRR/MM/DD HH24:MI:SS'),piv_program_name => 'TEST_MAIN()',piv_enabled => 'Y',piv_auto_drop => 'Y');dbms_output.put_line(lv_errbuf);END;
Testing:
Hope It helps!!
Do you think this Article is useful?
Subscribe to:
Post Comments (Atom)
Disclaimer
The ideas, thoughts and concepts expressed here are my own. They, in no way reflect those of my employer or any other organization/client that I am associated. The articles presented doesn't imply to any particular organization or client and are meant only for knowledge Sharing purpose. The articles can't be reproduced or copied without the Owner's knowledge or permission.
0 Responses to “Example Script to create Database Jobs (DBMS_SCHEDULER.CREATE_JOB)”
Post a Comment