oracle-databasestored-proceduresplsqljob-schedulingdbms-scheduler

create oracle job run procedure with parameters once


I want to create oracle job in specific date and time that runs only on that date and time once and then dropped automatically.

The job should runs a procedure with 2 parameters also .

note: l_id I used this variable to add number beside procedure name to avoid any kind of duplication.

P94_DATE: the user will choose date and time and it will be stored in that variable.

here what I found so far but when I run this , it gives me 'statement proceed' but when I check if the job created successfully or not on 'sys.all_scheduler_jobs' it doesn't exist.

dbms_scheduler.create_job (
job_name           =>  'eba_sb_reminder_s'||l_id,
job_type           =>  'STORED_PROCEDURE',
 job_action         =>  'BEGIN send_Schedule_reminders(1,2); END;',
 start_date         =>  :P94_DATE, -- I need to assign time also !!
 enabled            =>  true,
comments           => 'check if there is new reminders needs to be send in specific date and time'


 );
end;

Solution

  • When job_type is set to STORE_PROCEDURE you must specify the name of the procedure in job_action.

    The parameter start_date is of type DATE, which has a time as well in Oracle. You just need to set :p94_date with a correct value, containing a date and a time part.

    If the procedure has parameters, you need to use DBMS_SCHEDULER.set_job_argument_value to specify a parameter-value.

    Edit: Sample modified

    Sample:

    BEGIN
       -- This needs to be configured just once.
       DBMS_SCHEDULER.create_program(program_name          => 'test_program',
                                     program_type          => 'STORED_PROCEDURE',
                                     program_action        => 'test',
                                     number_of_arguments   => 2,
                                     enabled               => FALSE,
                                     comments              => 'Comment');
    
       DBMS_SCHEDULER.define_program_argument(program_name        => 'test_program',
                                              argument_name       => 'p1',
                                              argument_position   => 1,
                                              argument_type       => 'NUMBER',
                                              DEFAULT_VALUE       => NULL);
    
       DBMS_SCHEDULER.define_program_argument(program_name        => 'test_program',
                                              argument_name       => 'p2',
                                              argument_position   => 2,
                                              argument_type       => 'NUMBER',
                                              DEFAULT_VALUE       => NULL);
    
       DBMS_SCHEDULER.enable(name => 'test_program');
    
       -- Create job
       DBMS_SCHEDULER.create_job(
          job_name       => 'test_job',
          program_name   => 'test_program',
          start_date     => :p94_date,
          enabled        => FALSE,
          comments       => 'check if there is new reminders needs to be send in specific date and time');
    
       -- Set Procedure Parameter
       DBMS_SCHEDULER.set_job_argument_value(job_name => 'test_job', argument_position => 1, argument_value => 1);
       DBMS_SCHEDULER.set_job_argument_value(job_name => 'test_job', argument_position => 2, argument_value => 2);
    
       -- Enable job
       DBMS_SCHEDULER.enable(name => 'test_job');
    END;