Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass select statement to Oracle PLSQL

Tags:

oracle

plsql

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.

like image 844
user1771518 Avatar asked Oct 24 '12 14:10

user1771518


People also ask

Can we use SELECT statement in Plsql?

You can also use the Oracle SELECT statement to select individual fields from the table, as opposed to all fields from the table.

What is pass by value and pass by reference in Oracle PL SQL?

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.

How do I pass a parameter to a stored procedure in PL SQL?

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.

How do you execute a query stored in a variable in Oracle?

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.


1 Answers

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
like image 198
Vincent Malgrat Avatar answered Oct 18 '22 17:10

Vincent Malgrat