tags:

views:

1588

answers:

4

Trying to make a generic PL/SQL procedure to export data in specific XML format, e.g. Excel XML. Let's say the procedure accepts a string with the SELECT query to EXECUTE IMMEDIATE.

This requires access to data types of each column of the resulting rowset, which -- seeing as the procedure is to be generic -- is only known after the query is run.

I have tried an approach with a temporary table, but for the procedure to compile the table must exist and have its structure known at compile time.

How can I next process the rows and columns of an EXECUTE IMMEDIATE result in a double loop that analyzes the type of each value and emits an appropriate piece of XML?

+6  A: 

You can't do that with EXECUTE IMMEDIATE. You'll have to use the more powerful (and more complex) DBMS_SQL package - I've linked you to the DESCRIBE_COLUMNS procedure, which is especially relevant.

Tony Andrews
Ya, that should work.
EvilTeach
A: 

Or query ALL_TAB_COLS to get the column datatype.

cagcowboy
I had the same thought at first, but if the input is the full query, e.g. 'SELECT a, b, c FROM some_table', then you would have to parse out the column names from the query. Using DBMS_SQL is probably better since you can have Oracle do the parsing for you.
Dave Costa
+1  A: 

I'm having a similar problem. Using DBMSSQL.DESCRIBECOLUMNS I get column types but now I need to have variables declared before as I need them in DBMSSQL.DEFINECOLUMN to get columns' values. Is it possible to declare variable after I execute DESCRIBECOLUMNS?

rostek
A: 

try this link it helps me lot .. http://pravinmagdum.wordpress.com/2009/06/15/query-for-select-field-namesdata-types-etc/