【问题标题】:Array Input for Stored Procedure存储过程的数组输入
【发布时间】:2014-12-28 13:07:10
【问题描述】:

我是 Oracle 的新手,这是我关于 Oracle 查询的第一篇文章。

下面是现有的查询,它为每个 SP 调用插入 1 行。

我想在 SP 中进行更改,它将接受作为数组的输入,SAP 系统会将数组发送到存储过程。

正如您在 SP 中观察到的,ID 的值随着每次更新而每次递增。 SP 会接受这个 Phone 和 Text 的输入,并按顺序插入 ID 的值。ID 不会在输入中传递。

CREATE OR REPLACE PROCEDURE DetailsTable
(
    Phoneno IN NUMBER,
    Text IN VARCHAR2
    )
aS
BEGIN
   INSERT INTO PERSON.DETAILS(
                           ID,
                           PHONENO,
                           TEXT,
                           COUNTRY,
                           LANG,

                           --PRIORITY,
                           SENDER)
   VALUES (
           DETAILS_seq.nextval ,
           p_phoneno,
           p_text ,
           'RSA',
           'EN',
           'Nest-Payroll');
commit;
END DetailsTable;

请指导。

【问题讨论】:

    标签: sql oracle stored-procedures nested-table


    【解决方案1】:
    SQL> CREATE OR REPLACE TYPE arraytype AS VARRAY(1000) OF VARCHAR2(100);
      2  /
    
    Type created
    
    SQL> CREATE OR REPLACE PROCEDURE test_array (in_array arraytype) IS
      2  BEGIN
      3    FOR i IN 1..in_array.count LOOP
      4      DBMS_OUTPUT.PUT_LINE(in_array(i));
      5    END LOOP;
      6  END;
      7  /
    
    Procedure created
    
    SQL> DECLARE
      2    var_array arraytype;
      3  BEGIN
      4    var_array := arraytype();
      5    var_array.EXTEND(10);
      6    var_array(1) := '1st sentence in the array';
      7    var_array(2) := '2nd sentence in the array';
      8    test_array(var_array);
      9  END;
     10  /
    
    1st sentence in the array
    2nd sentence in the array
    

    【讨论】:

    • 这真的不能回答问题
    【解决方案2】:

    我们可以在 SQL 中使用类型,但需要将其声明为 SQL 类型:

    create or replace type person_t as object 
        (phoneno number
         , text varchar2(100)
         );
    /
    create or replace type person_nt as table of person_t
    /
    

    像这样使用它:

    CREATE OR REPLACE PROCEDURE DetailsTable
    (
        p_array in person_nt
        )
    aS
    BEGIN
       INSERT INTO PERSON.DETAILS(
                               ID,
                               PHONENO,
                               TEXT,
                               COUNTRY,
                               LANG,
    
                               --PRIORITY,
                               SENDER)
       select DETAILS_seq.nextval ,
               t.phoneno,
               t.text ,
               'RSA',
               'EN',
               'Nest-Payroll'
       from table (p_array)t;
       commit;
    END DetailsTable;
    /
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 2016-07-24
      • 1970-01-01
      相关资源
      最近更新 更多