Passing arguments to oracle stored procedure through scheduler job
Asked Answered
G

3

9

I have a stored procedure which is being called from a dbms job. i.e.

DBMS_SCHEDULER.RUN_JOB ('Procedure_JB', FALSE);

A java code stored procedure, which after doing some stuff, kicks off Procedure_JB asynchronously. And then this Procedure_JB calls Procedure_PRogram and then the program would call the stored procedure.

How can i pass arguments to my stored procedure?

  • The arguments which i have to pass to the job are from java.
Grind answered 7/2, 2012 at 16:18 Comment(0)
R
18

Define your job Procedure_JB to accept arguments. Then use dbms_scheduler.set_job_argument_value to define the value of the arguments you want to pass into the program your job is going to run. An example (taken from https://forums.oracle.com/forums/thread.jspa?threadID=483135)

-- create a stored procedure with two arguments
create or replace procedure myproc (arg1 in varchar2, arg2 in varchar2)
is BEGIN null; END;
/

-- create a program with two arguments and define both
begin
dbms_scheduler.create_program
(
program_name=>'myprog',
program_action=>'myproc',
program_type=>'STORED_PROCEDURE',
number_of_arguments=>2, enabled=>FALSE
) ;

dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>1,
argument_type=>'VARCHAR2',
DEFAULT_VALUE=>'13');

dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>2,
argument_type=>'VARCHAR2');

dbms_scheduler.enable('myprog');
end;
/

-- create a job pointing to a program and set both argument values
begin
dbms_scheduler.create_job('myjob',program_name=>'myprog');
dbms_scheduler.set_job_argument_value('myjob',1,'first arg');
dbms_scheduler.set_job_argument_value('myjob',2,'second arg');
dbms_scheduler.enable('myjob');
end;
/
Rad answered 8/2, 2012 at 5:19 Comment(1)
A nightmare might be @AlexanderMills, but that's the generic way to handle arbitrary programs with arguments other than VARCHAR2. If all arguments are of type VARCHAR2, then one could simply do what Steve G suggested.Statecraft
C
9

Use PLSQL_BLOCK instead:

begin
dbms_scheduler.create_job (
    job_name => 'myjob',
    job_type => 'PLSQL_BLOCK',
    job_action => 'BEGIN myproc(''first arg'',''second arg''); END;',
    start_date => sysdate,
    repeat_interval => 'FREQ=HOURLY',
    enabled => true    
);
end;
/
Capillarity answered 3/8, 2018 at 19:17 Comment(2)
I +rep this since it is the simpler choice,but only when the arguments are of type VARCHAR2. If the procedure expects something else (say, timestamp arguments), then this won't fly.Statecraft
If you need to run a job asynchronously , then skip the SYSDATE column.Fenestration
G
0

How it could work in version 19c (may be earlier)

BEGIN
    DBMS_SCHEDULER.CREATE_JOB (
    job_name => 'TMP_JOB_'||'test_procedure_job', -- customize job name
    job_type => 'STORED_PROCEDURE',
    job_action => 'PACKAGE_NAME.test_procedure',
    number_of_arguments => 2,
    start_date => NULL,
    repeat_interval => NULL,
    end_date => NULL,
    enabled => FALSE,
    auto_drop => TRUE, -- in my case onetimer object - not to store in DB schema
    comments => '');
    
    dbms_scheduler.set_job_argument_value(job_name => 'TMP_JOB_'||'test_procedure_job',
                      argument_position => 1,
                      argument_value => ''||'text_arg');
                     
    dbms_scheduler.set_job_argument_value(job_name => 'TMP_JOB_'||'test_procedure_job',
                      argument_position => 2,
                      argument_value => 100500); -- number arg
    
    DBMS_SCHEDULER.enable(name => 'TMP_JOB_'||'test_procedure_job');
END;
Goya answered 10/10, 2023 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.