为Oracle dbms_scheduler作业存储过程设置命名参数

时间:2013-01-22 08:53:31

标签: stored-procedures plsql oracle10g named-parameters dbms-scheduler

是否可以将命名参数传递给类型为“stored_procedure”的dbms_scheduler作业?我试过这种方式:

-- 1) example dummy procdure
CREATE OR REPLACE PROCEDURE my_test_proc (
 param1 IN NVARCHAR2,
 param2 IN NUMBER,
 param3 IN NUMBER
) IS
BEGIN
-- ...
END;

-- 2)Example dummy job:
BEGIN    
    dbms_scheduler.create_job(
        job_name => 'my_test_job'
        ,job_type => 'STORED_PROCEDURE'
        ,job_action => 'my_test_proc'
        ,start_date => sysdate
        ,number_of_arguments => 3
        ,enabled => FALSE
        ,auto_drop =>FALSE
    );
END;
-- 3)Set named param value:
BEGIN  
    dbms_scheduler.set_job_argument_value(
        job_name => 'my_test_job'
        ,argument_name => 'param1' 
        ,argument_value => 'some value'
    );
END;  

我收到以下错误: ORA

  

-27484:没有程序的作业不支持参数名称。 ORA-06512:在“SYS.DBMS_ISCHED”,第207行ORA-06512:at   “SYS.DBMS_SCHEDULER”,第602行ORA-06512:第2行

通过使用argument_position设置,我使用set_job_argument_value成功设置了参数值。但是有些情况下我需要运行存储过程,我只需要设置某些参数,这样就无法工作了。有没有办法将命名参数传递给由调度程序作业破坏的存储过程?

2 个答案:

答案 0 :(得分:4)

作为错误状态,首先创建一个程序,然后创建一个程序。

dbms_scheduler.create_program(program_name        => 'YOUR_PROGRAM',
                              program_type        => 'STORED_PROCEDURE',                                                          
                              program_action      => 'my_test_proc', 
                              number_of_arguments => 2,
                              enabled             => false,
                              comments            => 'Comments you want');

dbms_scheduler.define_program_argument(program_name      => 'YOUR_PROGRAM',
                                       argument_name     => 'param1',
                                       argument_position => 1,
                                       argument_type     => 'VARCHAR2',
                                       default_value     => '');
    ..etc, do for all 3.                                         

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


dbms_scheduler.create_job(job_name        => 'my_test_job',
                          program_name    => 'YOUR_PROGRAM',
                          start_date      => systimestamp,
                          end_date        => null,
                          ...

dbms_scheduler.set_job_argument_value(job_name          => 'my_test_job',
                                      argument_position => 1,
                                      argument_value    => 'value');
  ...

答案 1 :(得分:1)

我有一个基本的程序示例

1.-创建我的程序

CREATE OR REPLACE PROCEDURE MY_TEST_PROCEDURE (P_VAR1 VARCHAR2, P_VAR_NUMBER NUMBER)
AS 
    V_RESULT_MSG VARCHAR2(300);
BEGIN
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'DD/MM/YYYY HH24:MI'));
    V_RESULT_MSG := P_VAR1;
    V_RESULT_MSG := V_RESULT_MSG||' '||P_VAR_NUMBER;

    DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'DD/MM/YYYY HH24:MI'));
    DBMS_OUTPUT.PUT_LINE(V_RESULT_MSG);
END MY_TEST_PROCEDURE;

2.-创建程序

BEGIN
    DBMS_SCHEDULER.CREATE_PROGRAM(
        PROGRAM_NAME => 'MY_TEST_PROGRAM'
        ,PROGRAM_TYPE => 'STORED_PROCEDURE'
        ,PROGRAM_ACTION => 'MY_TEST_PROCEDURE'
        ,NUMBER_OF_ARGUMENTS => 2
        ,ENABLED =>  FALSE
        ,COMMENTS => 'my test program'
    );

    DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(
        PROGRAM_NAME => 'MY_TEST_PROGRAM',
        argument_name     => 'P_VAR1',
        argument_position => 1,
        argument_type     => 'VARCHAR2',
        default_value     => ''
    );

    DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(
        PROGRAM_NAME => 'MY_TEST_PROGRAM',
        argument_name     => 'P_VAR_NUMBER',
        argument_position => 2,
        argument_type     => 'NUMBER',
        default_value     => ''
    );

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

END;

3.-创建工作

BEGIN
    DBMS_SCHEDULER.CREATE_JOB (
        job_name => '"XXFLM"."MY_TEST_JOB"',
        PROGRAM_NAME => 'MY_TEST_PROGRAM'
    );

END;

4.- PL / SQL代码测试

DECLARE
    V_VAR1 VARCHAR2(50) := 'HOLA';
    V_VAR2 NUMBER  := 2010;

BEGIN

    dbms_scheduler.set_job_argument_value(
        job_name          => 'MY_TEST_JOB',
        argument_position => 1,
        argument_value    => V_VAR1);

    dbms_scheduler.set_job_argument_value(
        job_name          => 'MY_TEST_JOB',
        argument_position => 2,
        argument_value    => V_VAR2);

    DBMS_SCHEDULER.RUN_JOB(
        JOB_NAME            => '"XXFLM"."MY_TEST_JOB"',
        USE_CURRENT_SESSION => FALSE);

END;

5.-结果

SELECT * FROM ALL_SCHEDULER_JOB_RUN_DETAILS ORDER BY 1 DESC;