2013-10-24 16 views
1

我正在使用有50/60個表格的系統。每個人都有相同的唯一密鑰(在此示例中稱爲MEMBID在有數據的地方查找表格

是否有可以運行的查詢將顯示至少有一行存在MEMBID的所有表的名稱?

或者我需要通過USER_TABLES表的光標,然後建立一個動態查詢來建立一個「數組」?

非常感謝

邁克

+0

會這樣嗎? select * from user_tables,user_constraints c where rownum> 0 and c.CONSTRAINT_NAME ='MEMBID' – Moudiz

+1

@Moudiz這將列出約束存在的所有表,但它不會告訴你表中是否至少包含一行帶有非空的MEMBID。 –

回答

1

我會去動態SQL - 這是非常簡單的:

declare 
    l_cnt_membid number; 
    l_cnt_overall number; 
begin 
    for cur in (select table_name from user_tab_cols where column_name = 'MEMBID') 
    loop 
     execute immediate 'select count(*), count(membid) from ' || cur.table_name 
     into l_cnt_overall, l_cnt_membid; 
     dbms_output.put_line(cur.table_name || ', overall: ' || l_cnt_overall || 
     ', membid: ' || l_cnt_membid); 
    end loop; 
end; 

編輯:

如果您的表的統計信息是最新的,您可以直接從user_tab_cols獲取此信息:

select table_name, 
    (case when num_distinct > 0 
    then 'YES' 
    else 'NO' end) has_nonnull_membid 
from user_tab_cols 
where column_name = 'MEMBID'