我很晚才回答,但无论如何我会为后代添加这个。
在 Oracle 中,您可以使用 DBMS_SQL 包。
用法:
- 将您的 SQL 语句转换为单行(用空格替换换行符)
- 用两个单引号替换单引号
- 将结果值放入 STMT(在下面的脚本中)。
- 运行脚本。
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;
我没有在下面的脚本中涵盖所有可能的数据类型,如果需要,您可以添加更多案例。您可以使用以下 SQL 了解数据类型值:
select text
from all_source
where owner = 'SYS' and name = 'DBMS_TYPES' and type = 'PACKAGE'
Toad World 在这里有一个很好的可能数据类型列表:
http ://www.toadworld.com/platforms/oracle/w/wiki/3328.dbms-sql-describe-columns
除了这个线程,我在 Oracle 上找不到相同的列表:
https ://community.oracle.com/thread/914475
示例:
-- snip
STMT := 'SELECT * FROM SYS.ALL_TAB_COLS';
-- snip
会给你:
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)