Reputation: 51
I have a PLSQL oracle function that accepts an array:
CREATE OR REPLACE FUNCTION CM.give_me_an_arrays (p_array IN num_array)
RETURN VARCHAR2
IS
x VARCHAR2 (512);
BEGIN
x := '';
FOR i IN 1 .. p_array.COUNT
LOOP
DBMS_OUTPUT.put_line (p_array (i));
END LOOP;
RETURN x;
END;
/
I want to do that:
select CM.give_me_an_arrays(select COM.COM_ID
from CM.XLP_SE_COMPONENT com
where rownum < 10)
from dual
Any Ideas? Thanks in advance.
Upvotes: 5
Views: 1740
Reputation: 67722
You can do this as long as the array is an sql object (tested on 11gR2, should work on 10g):
SQL> create or replace type num_array is table of number;
2 /
Type created.
SQL> CREATE OR REPLACE FUNCTION give_me_an_arrays (p_array IN num_array)
2 RETURN VARCHAR2
3 IS
4 x VARCHAR2 (512);
5 BEGIN
6 x := '';
7 FOR i IN 1 .. p_array.COUNT
8 LOOP
9 DBMS_OUTPUT.put_line (p_array (i));
10 END LOOP;
11
12 RETURN x;
13 END;
14 /
Function created.
You can call this function with the COLLECT aggregate function:
SQL> SELECT give_me_an_arrays((SELECT cast(collect(rownum) AS num_array) value
2 FROM dual
3 CONNECT BY level <= 10)) arr
4 FROM dual;
ARR
--------------------------------------------------------------------------------
1
2
[..]
10
In 9i (and maybe even 8i, can't test right now), COLLECT didn't exist but you could have used MULTISET instead:
SQL> SELECT give_me_an_arrays(cast(MULTISET(SELECT rownum value
2 FROM dual
3 CONNECT BY level <= 10) AS num_array)
4 ) arr
5 FROM dual;
ARR
--------------------------------------------------------------------------------
1
2
[..]
10
Upvotes: 6