简体   繁体   English

是否可以在 Oracle dbms_scheduler 中传递自定义类型的参数?

[英]Is it possible to pass a custom type of argument in Oracle dbms_scheduler?

I have a collection (associative array or nested table) of NUMBER variables.我有 NUMBER 个变量的集合(关联数组或嵌套表)。 I want to initiate a job which invokes a stored procedure which in turn receives this very custom type and does something for each element.我想启动一个调用存储过程的作业,该存储过程反过来接收这种非常自定义的类型并为每个元素做一些事情。 Now my job takes a program where I set arguments.现在我的工作需要一个设置 arguments 的程序。 How do I set the argument of my custom data type (associative array or table of Number) into the scheduler program?如何将自定义数据类型(关联数组或数字表)的参数设置到调度程序中? Or any alternative ways?或者有什么替代方法? Thanks in advance提前致谢

Custom types can be passed to scheduler programs using ANYDATA .可以使用ANYDATA将自定义类型传递给调度程序。 But the process is complicated, so it might be easier to create a new job with a hard-coded PL/SQL block instead of re-using a program with arguments.但是这个过程很复杂,因此使用硬编码的 PL/SQL 块创建新作业可能比使用 arguments 重新使用程序更容易。

--Create a nested table of NUMBERs.
create or replace type number_nt is table of number;

--Create a procedure that accepts a nested table of numbers.
create or replace procedure test_procedure(p_numbers in number_nt) is
begin
    if p_numbers is not null then
        for i in 1 .. p_numbers.count loop
            dbms_output.put_line(p_numbers(i));
        end loop;
    end if;
end;
/

--Create a PROGRAM to run the stored procedure.
begin
    dbms_scheduler.create_program
    (
        program_name        => 'TEST_PROGRAM',
        program_type        => 'STORED_PROCEDURE',
        program_action      => 'TEST_PROCEDURE',
        number_of_arguments => 1
    );
end;
/

--Define the argument that will be passed into the stored procedure and enable the program.
begin
    dbms_scheduler.define_anydata_argument
    (
        program_name      => 'TEST_PROGRAM',
        argument_position => 1,
        argument_type     => 'SYS.ANYDATA',
        default_value     => null
    );

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

--Create the nested table of numbers, convert it into an ANYDATA, create a job,
--pass the ANYDATA to the job, and then enable the job.
declare
    v_numbers number_nt := number_nt(1,2,3);
    v_anydata anydata;
begin
    v_anydata := anydata.convertCollection(v_numbers);

    dbms_scheduler.create_job
    (
        job_name     => 'TEST_JOB',
        program_name => 'TEST_PROGRAM',
        enabled      => false
    );

    dbms_scheduler.set_job_anydata_value
    (
        job_name          => 'TEST_JOB',
        argument_position => 1,
        argument_value    => v_anydata
    );

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

--Check the results.
--STATUS = "SUCCEEDED", OUTPUT = "1 2 3"
select status, output
from dba_scheduler_job_run_details where job_name = 'TEST_JOB';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM