How to schedule a work to run at particular time using dbms_scheduler

10,065

create_job is the basic call to schedule a call. you don't have to create a named program or schedule to do this. where creating a named program/schedule is useful, is if you have several jobs that want to use this call. you can just reference the named program schedule instead of having each job hold a copy of that.

e.g. if you had 5 jobs wanting to call your package MYPKG.ENTRY_PROG(param) and each job just used a different parameter value, i'd say you want to use create_program to define that pl/sql call and then create_job to reference that program name + set the parameter value of choice. that way, if you want to rename the API later or something, you don't have to change five seperate jobs to do this.

If your job is just a standalone job that calls a routine that won't be called by other jobs, then you don't have to use create_program/create_schedule, just use create_job directly.

one example where I used create_program was to call a test harness. my test harness package is called pkg_test_harness.queue_tests(p_set_name in varchar2) so I have a few jobs defined that enqueues various APIs to be run at 9AM, 12PM and 5PM. instead of defining each jobs call separately i just called create_program like:

    dbms_output.put('Setting up TEST_HARNESS_ENQUEUE scheduler program...');
    dbms_scheduler.create_program(program_name        => 'TEST_HARNESS_ENQUEUE',
                                  program_type        => 'STORED_PROCEDURE',                                                          
                                  program_action      => 'pkg_test_harness.queue_tests', 
                                  number_of_arguments => 1,
                                  enabled             => false,
                                  comments            => 'Program to enqueue a set of API test for the test harness to run.');

    dbms_scheduler.define_program_argument(program_name      => 'TEST_HARNESS_ENQUEUE',
                                           argument_name     => 'p_set_name',
                                           argument_position => 1,
                                           argument_type     => 'VARCHAR2',
                                           default_value     => '');

    dbms_scheduler.enable (name => 'TEST_HARNESS_ENQUEUE');

    dbms_output.put_line('done.');

and then each "job" was defined pointing to the program.

dbms_output.put('Setting up TEST_HARNESS_ENQUEUE_9AM scheduler job...');
dbms_scheduler.create_job(job_name        => 'TEST_HARNESS_ENQUEUE_9AM',
                          program_name    => 'TEST_HARNESS_ENQUEUE',
                          start_date      => systimestamp,
                          end_date        => null,
                          repeat_interval => 'freq=daily; byhour=9; byminute=0; bysecond=0;',
                          enabled         => true,
                          auto_drop       => false,
                          comments        => 'Job to enqueue a set of API test for the test harness to run.');

dbms_scheduler.set_job_argument_value(job_name          => 'TEST_HARNESS_ENQUEUE_9AM',
                                      argument_position => 1,
                                      argument_value    => 'DAILY_9AM');
dbms_output.put_line('done.');

dbms_output.put('Setting up TEST_HARNESS_ENQUEUE_12PM scheduler job...');
dbms_scheduler.create_job(job_name        => 'TEST_HARNESS_ENQUEUE_12PM',
                          program_name    => 'TEST_HARNESS_ENQUEUE',
                          start_date      => systimestamp,
                          end_date        => null,
                          repeat_interval => 'freq=daily; byhour=12; byminute=0; bysecond=0;',
                          enabled         => true,
                          auto_drop       => false,
                          comments        => 'Job to enqueue a set of API test for the test harness to run.');

i didn't create a named schedule, as these schedules are unique to the individual job.

Share:
10,065
user1
Author by

user1

Updated on June 14, 2022

Comments

  • user1
    user1 almost 2 years

    Im not clear about this, here in DBMS_SCHEDULER we have CREATE_PROGRAM CREATE_JOB CREATE_SCHEDULE etc., after reading the oracle doc still im unclear what to use, On the Oracle side, i am going to use DBMS_SCHEDULER to insert a new message into the queue at the appropriate time, i planned to create scheduler to execute it on particular time and then create program to execute my PL/SQL block which will enqueue the message in the queue Or instead of using CREATE_SCHEDULE and CREATE_PROGRAM, CREATE_JOB does both the jobs, which to use? please guide me whether i am doing correctly, if not please correct me.

    Thankyou

  • user1
    user1 over 11 years
    Hi Thanks for the reply. Please consider the scenario. "Multiple merchants has logged in the portal, and have configured the settlement time of the transactions at a particular time. The settlement time configured by the merchants will vary across the merchants, and is not a single fixed time. Multiple events or notifications has to be generated at the timming configured by the merchants only.1.)In that case only create_job will help me?2.)The same create_job can also be reused by other merchants also right? Hope i am able to explain the problem. Please revert with any suitable solution.
  • DazzaL
    DazzaL over 11 years
    @user1 from your description, as each merchant is specifying their own schedule, then yes, a named schedule isn't needed. but if the pl/sql proc is the same for each merchant, then create_program + create_job is the way to go
  • user1
    user1 over 11 years
    What is the need to go for create_program when we have job_type and job_action in create_job?
  • DazzaL
    DazzaL over 11 years
    @user1 i put the reason in my original answer. you dont have to use create_program it's just perhaps neater to do so if the pl/sql call is shared between jobs. if you fine it easier for you to just use create_job with a job_action then by all means, do so.