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.
You can also use the Oracle SELECT statement to select individual fields from the table, as opposed to all fields from the table.
Actual parameter must be a variable. Actual parameter must be a variable. Actual parameter is passed by reference (a pointer to the value is passed in) Actual parameter is passed by value (a copy of the value is passed out) unless NOCOPY is specified.
In PL/SQL, we can pass parameters to procedures and functions in three ways. 1) IN type parameter: These types of parameters are used to send values to stored procedures. 2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions.
You could do it in plain SQL with a SELECT statement. But, if you are really doing it in PL/SQL, then you need to (ab)use EXECUTE IMMEDIATE. Also, you would want to retrieve the output of the SELECT statement in PL/SQL, for which Oracle expects an INTO clause.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With