2014-11-01 117 views
0

我是Oracle的新手,这是我第一篇针对Oracle查询的文章。存储过程的数组输入

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

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

正如您在SP中所观察到的,每次更新时,ID的值都会递增。 SP将接受电话和文本的输入,并按顺序插入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; 

请指导。

回答

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 
+0

这真的没有回答题 – APC 2014-11-01 14:55:51

1

我们可以使用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; 
/