简体   繁体   English

如何获取查询Oracle SQL返回的假设表的列名

[英]How to get column names of a hypothetical table returned by a query oracle sql

Suppose I have a complicated SQL query like 假设我有一个复杂的SQL查询,例如

select *
from mydbadmin.tablename
inner join mydbadmin.othertablename
on mydbadmin.tablename.id = mydbadmin.othertablename.id
where mydbadmin.tablename.y = 2

What query can I make to get the headers of the table running this query would return? 我可以进行什么查询来获取运行该查询的表的标题?

Notes: 笔记:

I've checked Oracle query to fetch column names , but that doesn't seem to help because I'm not trying to find the names from an existing table*. 我已经检查了Oracle查询以获取列名 ,但这似乎无济于事,因为我没有尝试从现有表中查找名称*。

*I have no ability to actually create this table, so making it and then running the ordinary query isn't an option. *我实际上无法创建此表,因此使其无法选择然后再运行普通查询是不可行的。

You may use the DESCRIBE_COLUMNS Procedure of the DBMS_SQL package 您可以使用DBMS_SQL程序包的DESCRIBE_COLUMNS过程

Here are the critical steps: open cursor, parse it and execute. 这是关键步骤:打开游标,解析并执行。 Finaly call the describe_columns , which returns the definition in DBMS_SQL.DESC_TAB 最后调用describe_columns ,它返回DBMS_SQL.DESC_TAB的定义

  c := DBMS_SQL.OPEN_CURSOR;

  DBMS_SQL.PARSE(c, q'[SELECT 'x' col1, sysdate col2, 1 col3 FROM dual]', DBMS_SQL.NATIVE);

  d := DBMS_SQL.EXECUTE(c);

  DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);

The sample output for the first column of the above query is 上面查询的第一列的示例输出是

col_type            =    96
col_maxlen          =    1
col_name            =    COL1
col_name_len        =    4
col_schema_name     =    
col_schema_name_len =    0
col_precision       =    0
col_scale           =    0
col_null_ok         =    true

The full example see in Oracle Documentation 完整示例参见Oracle文档

As an alternative you may use JDBC resultSet.getMetaData() method. 或者,您可以使用JDBC resultSet.getMetaData()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM