2017-04-13 79 views
0

我有以下查询,它返回包含列PROGRAM_ID列的所有表。有没有办法可以在包含列PROGRAM_ID的数据库中的所有表上返回所有不同的值?获取跨多个表的特定列的所有不同值

select table_name from all_tab_columns where column_name = 'PROGRAM_ID'; 
+1

我不认为这是可能的,而无需使用'工会all'通过返回的所有表:这将使用SQL * Plus和SQL Developer的variableprint命令打开具有结果的参考指针,在这个演示您的查询。 –

+0

你可以通过动态SQL来完成 –

回答

1

您可以生成一个SQL查询,您可以再复制和手工粘贴:

select case when rownum > 1 then 'union ' end 
    || 'select program_id from ' || owner || '.' || table_name 
from all_tab_columns where column_name = 'PROGRAM_ID'; 

产生的输出,如:

select program_id from SYS.V_$SQLAREA_PLAN_HASH 
union select program_id from SYS.V_$SQLAREA 
union select program_id from SYS.V_$SQL 
union select program_id from SYS.GV_$SQLAREA 
union select program_id from SYS.GV_$SQLAREA_PLAN_HASH 
union select program_id from SYS.GV_$SQL 
union select program_id from MY_SCHEMA.TABLE_A 
union select program_id from MY_SCHEMA.TABLE_B 
union select program_id from MY_SCHEMA.TABLE_C 

所以你可能要筛选的用户是检索;或者如果您只对自己的模式中的表感兴趣,则切换到user_tab_columns(并丢失owner部分)。

如果您想一次性识别和查询表,您可以做同样的事情,但作为动态SQL。

var rc refcursor; 

declare 
    l_stmt clob; 
begin 
    select listagg(case when rownum > 1 then 'union ' end 
    || 'select program_id from ' || owner || '.' || table_name, ' ') 
    within group (order by null) 
    into l_stmt 
    from all_tab_columns where column_name = 'PROGRAM_ID'; 

    open :rc for l_stmt; 
end; 
/

print rc 
相关问题