简体   繁体   English

在Oracle中,是否可以查询具有相同数据类型的列

[英]in oracle, is it possible to query columns with same data type

I have table which has almost hundred fields. 我有近一百个字段的表。 I want to get all the fields which data type is date. 我想获取所有数据类型为日期的字段。 Is it possible in oracle to write such a query to return fields only contain a certain data type? 在oracle中是否可以编写这样的查询以返回仅包含某种数据类型的字段? Here is my pseudo query: 这是我的伪查询:

Select * from mytable 
where colum_datatype is date

Similarly, I want to get all fields which is varchar2 type. 同样,我想获取varchar2类型的所有字段。 is it possible to do that? 有可能这样做吗? I can find all the date fields manually and put them in the query but I just want to know is there another way to do it. 我可以手动找到所有日期字段,并将其放入查询中,但我只想知道还有另一种方法。 Thank you! 谢谢!

You can query one of the system tables/views to get the list of columns: 您可以查询系统表/视图之一以获取列列表:

select column_name
from all_tab_cols
where owner = :owner and table_name = :table and data_type = 'DATE';

If you need a one-off solution, just aggregate these and plug into a sql query. 如果您需要一次性解决方案,只需将其汇总并插入sql查询即可。 You can construct the entire SQL query: 您可以构造整个SQL查询:

select 'SELECT ' || listagg(column_name, ', ') within group (order by column_id) || ' FROM ' || :table
from all_tab_cols
where owner = :owner and table_name = :table and data_type = 'DATE';

You can also put the query into a string and use dynamic SQL ( execute immediate ) to run the query. 您还可以将查询放入字符串中,并使用动态SQL( execute immediate )运行查询。

Sorry, no such functionality exists in vanilla SQL. 抱歉,香草SQL中不存在此类功能。 You may be able to simulate such functionality by creating a PL/SQL function that returns a cursor to a dynamically created SQL statement. 通过创建将游标返回到动态创建的SQL语句的PL / SQL函数,您可能能够模拟此类功能。

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

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