2014-11-01 110 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; 
/