I'm very late to answer, but anyway I'll add this for posterity.
In Oracle, you can use the DBMS_SQL package.
Usage:
- Convert your SQL statement into a single line (replacing newline with space)
- Replace single quote with two single quotes
- Put the resulting value into STMT (in the below script).
- Run the script.
SET SERVEROUTPUT ON;
DECLARE
STMT CLOB;
CUR NUMBER;
COLCNT NUMBER;
IDX NUMBER;
COLDESC DBMS_SQL.DESC_TAB2;
BEGIN
CUR := DBMS_SQL.OPEN_CURSOR;
STMT := '';
SYS.DBMS_SQL.PARSE(CUR, STMT, DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS2(CUR, COLCNT, COLDESC);
DBMS_OUTPUT.PUT_LINE('Statement: ' || STMT);
FOR IDX IN 1 .. COLCNT
LOOP
CASE COLDESC(IDX).col_type
WHEN 2 THEN
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': NUMBER');
WHEN 12 THEN
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': DATE');
WHEN 180 THEN
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': TIMESTAMP');
WHEN 1 THEN
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': VARCHAR');
WHEN 9 THEN
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': VARCHAR2');
-- Insert more cases if you need them
ELSE
DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': OTHERS (' || TO_CHAR(COLDESC(IDX).col_type) || ')');
END CASE;
END LOOP;
SYS.DBMS_SQL.CLOSE_CURSOR(CUR);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE()) || ': ' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
SYS.DBMS_SQL.CLOSE_CURSOR(CUR);
END;
I did not cover all possible data types in the below script, you can add more cases if you need them. You can find out about the data type values by using this SQL:
select text
from all_source
where owner = 'SYS' and name = 'DBMS_TYPES' and type = 'PACKAGE'
Toad World has a nice list of the possible data types here:
http://www.toadworld.com/platforms/oracle/w/wiki/3328.dbms-sql-describe-columns
I can't find the same list on Oracle except in this thread:
https://community.oracle.com/thread/914475
Example:
-- snip
STMT := 'SELECT * FROM SYS.ALL_TAB_COLS';
-- snip
Will give you:
anonymous block completed
Statement: SELECT * FROM SYS.ALL_TAB_COLS
#1: VARCHAR2 (1)
#2: VARCHAR2 (1)
#3: VARCHAR2 (1)
#4: VARCHAR2 (1)
#5: VARCHAR2 (1)
#6: VARCHAR2 (1)
#7: NUMBER
#8: NUMBER
#9: NUMBER
#10: VARCHAR2 (1)
#11: NUMBER
#12: NUMBER
#13: VARCHAR2 (8)
#14: NUMBER
#15: VARCHAR2 (23)
#16: VARCHAR2 (23)
#17: NUMBER
#18: NUMBER
#19: NUMBER
#20: DATE
#21: NUMBER
#22: VARCHAR2 (1)
#23: NUMBER
#24: VARCHAR2 (1)
#25: VARCHAR2 (1)
#26: NUMBER
#27: NUMBER
#28: VARCHAR2 (1)
#29: VARCHAR2 (1)
#30: VARCHAR2 (1)
#31: VARCHAR2 (1)
#32: VARCHAR2 (1)
#33: NUMBER
#34: NUMBER
#35: VARCHAR2 (1)
#36: VARCHAR2 (1)
DBMS_SQL.DESCRIBE
should do what you want: docs.oracle.com/cd/E11882_01/appdev.112/e25788/… – Bingham