4

是否可以将命名参数传递给类型为 '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:在“SYS.DBMS_SCHEDULER”,第 602 行 ORA-06512:在第 2 行

我通过使用 argument_position 设置成功地设置了 set_job_argument_value 参数值。但是在某些情况下,我需要运行存储过程,而我只需要为其设置某些参数,而这不起作用。有没有办法将命名参数传递给调度程序作业运行的存储过程?

4

2 回答 2

8

如错误所述,首先创建一个程序,然后再创建一个作业。

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');
  ...
于 2013-01-22T09:55:38.890 回答
3

我有一个程序的基本示例

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;
于 2020-04-29T17:04:27.827 回答