I have a dynamic select script which I build using pl/slq and I need to execute it in simple SQL.
How can I do these?
P.S.: I can't use views, because number of collumn's in returned table changes every time.
I have a dynamic select script which I build using pl/slq and I need to execute it in simple SQL.
How can I do these?
P.S.: I can't use views, because number of collumn's in returned table changes every time.
IF XML suites you well, then you can use DBMS_XMLGEN
Could be something like this:
select dbms_xmlgen.getxmltype('here comes your dynamic sql string').getstringval()
from dual;
If you don't know column set in advance this makes things complex. You can use such thing as XMLSequence + ref cursor, but again this returns XML-structured output you will need to treat somehow.
SQL> create or replace function get_rows
2 (tab_name varchar2)
3 return sys_refcursor
4 is
5 c sys_refcursor;
6 begin
7 open c for 'select * from ' ||tab_name;
8 return c;
9 end;
10 /
SQL> select * from table(xmlsequence(get_rows('dual')));
COLUMN_VALUE
--------------------------------------------------------------------------------
<ROW>
<DUMMY>X</DUMMY>
</ROW>
SQL> select * from table(xmlsequence(get_rows('T')));
COLUMN_VALUE
--------------------------------------------------------------------------------
<ROW>
<X>1</X>
<Y>A1</Y>
</ROW>
<ROW>
<X>2</X>
<Y>A2</Y>
</ROW>
<ROW>
<X>3</X>
<Y>A3</Y>
</ROW>