How to use pass an array in PL/SQL function

前端 未结 1 744
误落风尘
误落风尘 2021-02-04 14:18

I am a Java developer with limited knowledge of Oracle PL/SQL. Please let me know how to pass an array to a PL/SQL function in the following example and how to invoke it.

<
1条回答
  •  有刺的猬
    2021-02-04 14:36

    You can create a collection type and pass the parameter as an instance of that type.

    SQL> create type num_array as table of number;
      2  /
    
    Type created.
    
    SQL> create or replace function myfun ( arr_in num_array ) return varchar2 is
      2      txt varchar2(1000);
      3  begin
      4      for i in 1..arr_in.count loop
      5          txt := txt || to_char( arr_in(i) ) || ',';
      6      end loop;
      7      return txt;
      8  end;
      9  /
    
    Function created.
    
    SQL> declare
      2    myarray num_array;
      3    mytext  varchar2(1000);
      4  begin
      5    myarray := num_array();
      6    myarray.extend(3);
      7    myarray(1) := 1;
      8    myarray(2) := 5;
      9    myarray(3) := 9;
     10    dbms_output.put_line( myfun( myarray ));
     11  end;
     12  /
    
    1,5,9,
    
    PL/SQL procedure successfully completed.
    

    0 讨论(0)
提交回复
热议问题