动态选择要在SELECT语句中使用的列 [英] Dynamically select the columns to be used in a SELECT statement
本文介绍了动态选择要在SELECT语句中使用的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够使用系统表(在这种情况下为Oracle)来驱动SELECT语句中使用的字段.像这样:
I would love to be able to use the system tables (Oracle in this case) to drive which fields are used in a SELECT statement. Something like:
SELECT
(
select column_name
from all_tab_cols
where table_Name='CLARITY_SER'
AND OWNER='CLARITY'
AND data_type='DATE'
)
FROM CLARITY_SER
此语法不起作用,因为子查询返回多行,而不是一行包含多列.
This syntax doesn't work, as the subquery returns multiple rows, instead of one row with multiple columns.
是否可以通过查询表模式信息以仅选择某些列来动态生成SQL语句?
Is it possible to generate a SQL statement dynamically by querying the table schema information in order to select only certain columns?
**编辑** 如果可能,请在不使用函数或步骤的情况下执行此操作.
** edit ** Do this without using a function or procedure, if possible.
推荐答案
您可以执行以下操作:
declare
l_sql varchar2(32767);
rc sys_refcursor;
begin
l_sql := 'select ';
for r in
( select column_name
from all_tab_cols
where table_Name='CLARITY_SER'
AND OWNER='CLARITY'
AND data_type='DATE'
)
loop
l_sql := l_sql || r.column_name || ',';
end loop;
l_sql := rtrim(l_sql,',') || ' from clarity_ser';
open rc for l_sql;
...
end;
这篇关于动态选择要在SELECT语句中使用的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文