I want to execute dynamic query in my pipelined function and return results of this query. Is it possible to do this? Pipelined function is convenient for me to achieve good interface for my application cause it behaves like a table.
The function:
CREATE OR REPLACE FUNCTION MyFunction(p_schema VARCHAR2) RETURN MyTableType Pipelined IS
v_query VARCHAR2(1000);
BEGIN
v_query := 'SELECT * FROM TABLE ('||p_schema||'.somepackage.SomeFunction)'; --SomeFunction is another pipelined function
EXECUTE IMMEDIATE v_query;
--Results of the v_query are compatible with MyTableType's row type. But how to return them from pipelined function?
END;
It is possible to combine dynamic SQL and pipelined function but the return type will not be dynamic: the number and type of columns returned will be fixed.
You can use
EXECUTE IMMEDIATE
withBULK COLLECT
(thanks @be here now), dynamic cursors orDBMS_SQL
to return more than one row. Here's an example with a dynamic cursor:Let's call this dynamic function:
As always with dynamic SQL, beware of SQL Injection.
I think something like this:
Works only if v_query returns 1 row.
I couldn't get @VincentMalgrat's answer to work. But it was very close. Definitely a big help in the right direction for me.
Here's what I got to work:
Package
Package Body