我尝试将一组列标题的值作为文本返回,类似的话,如果您要使用listagg函数,但是,这些字段可以是用户定义的,不一定位于表中,因此使用dba_tab_cols上的lisatagg不是一个选项。
我正在使用的一段代码示例如下
with test_data1 as (select '2012' extract_yr, '01' extract_mth, 'John Smith' person1, 'Jane Doe' person2 from dual)
,test_data2 as (select '2016' extract_yr, '01' extract_mth, 'John Smith' person1, 'Jane Doe' person2 from dual)
select t1.extract_yr year
,t1.extract_mth month
,t1.person1 first_person
,t1.person2 second_person
from test_data1 t1
union all
select t2.extract_yr year
,t2.extract_mth month
,t2.person1 first_person
,t2.person2 second_person
from test_data2 t2
我之后将标题放入逗号分隔的输出/变量中,因此我可以在某些动态SQL中稍微使用它们。 所以在上面的例子中,我希望看到:
YEAR, MONTH, FIRST_PERSON, SECOND_PERSON
这是我正在编写的脚本自动化程序,因此输出需要以逗号分隔的方式进入clob。将执行数百个脚本,输出字段因脚本而异。
非常感谢任何正确方向的帮助或指示
答案 0 :(得分:1)
第二种方法是使用SQLData
dbms_sql.describe_columns
答案 1 :(得分:0)
你的意思是这样吗?
SELECT LISTAGG(Column_Name, ', ')
WITHIN GROUP (ORDER BY GROUP) as ColumnNames
FROM all_tab_cols
WHERE table_name = 'TableName'
AND owner = 'OwnerName'
如果除了结果之外还想要它,只需加入它们。
答案 2 :(得分:0)
我不确定,但也许对你有用。
首先我们创建新的临时表
create GLOBAL TEMPORARY table mytmp
ON COMMIT DELETE ROWS
as with test_data1 as (select '2012' extract_yr, '01' extract_mth, 'John Smith' person1, 'Jane Doe' person2 from dual)
,test_data2 as (select '2016' extract_yr, '01' extract_mth, 'John Smith' person1, 'Jane Doe' person2 from dual)
select t1.extract_yr year
,t1.extract_mth month
,t1.person1 first_person
,t1.person2 second_person
from test_data1 t1
union all
select t2.extract_yr year
,t2.extract_mth month
,t2.person1 first_person
,t2.person2 second_person
from test_data2 t2;
然后提取我们的专栏
SELECT LISTAGG(Column_Name, ', ') WITHIN GROUP (ORDER BY Column_Name)
FROM all_tab_cols
WHERE table_name = upper('mytmp');
和删除表
drop table mytmp;