Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL-SQL: getting column data types out of query results

Tags:

oracle

plsql

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?

like image 920
ttarchala Avatar asked Dec 17 '08 18:12

ttarchala


People also ask

How do I find the data type of a column in PL SQL?

Another way to get a column's data type is to query the ALL_TAB_COLUMNS view: SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE FROM ALL_TAB_COLUMNS WHERE OWNER = 'HR' AND TABLE_NAME = 'COUNTRIES';

How do I find the datatype of a table in Oracle?

ALL_TAB_COLUMNS is a view in Oracle that contains the information about all columns in all table. We can just query with the table name in this view to get the column names and data types of a table in Oracle.

How do I remove a special character from a query in PL SQL?

In Oracle SQL, you have three options for replacing special characters: Using the REPLACE function. Using the REGEXP_REPLACE function. Using the TRANSLATE function.


1 Answers

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.

like image 145
Tony Andrews Avatar answered Oct 20 '22 14:10

Tony Andrews