2013-02-20 28 views

回答

3

您可以通過加入ALL_TABLES來檢查表是否存在(同義詞可能不在同一模式的表中)。

select * 
    from all_synonyms s 
    left outer join all_tables t 
    on s.table_owner = t.owner 
    and s.table_name = t.table_name 
where s.owner = user 

添加條件and t.table_name is null如果您想要那些表不存在的同義詞。

如果要檢查同義詞是否爲VALID查詢ALL_OBJECTS

select * 
    from all_synonyms s 
    join all_objects o 
    on s.owner = o.owner 
    and s.synonym_name = o.object_name 
where o.object_type = 'SYNONYM' 
    and s.owner = user 
    and o.status <> 'VALID' 

由於a_horse_with_no_name在評論中指出,有一個同義詞是一個表,視圖,序列連包裝都是有效的沒有要求。

所以,你可能要更改第一個查詢查找這些還有:

select * 
    from all_synonyms s 
    join all_objects o 
    on s.table_owner = o.owner 
    and s.table_name = o.object_name 
where s.owner = user 
+0

要完成的圖片:一個同義詞是不一定是桌子。因此,如果同義詞指向例如第一個查詢可能會產生錯誤的否定結果。以觀看或程序。 – 2013-02-20 15:25:26

+0

真@a_horse_with_no_name,太集中在這個問題上。我已經更新了我的答案;謝謝。 – Ben 2013-02-20 15:29:03

0

使用下面的查詢:

select s.table_owner, s.table_name 
from all_synonyms s, all_tables t 
where s.table_owner = t.owner(+) 
and s.table_name = t.table_name(+) 
and t.owner is null 
--s.owner = 'SCHEMA_NAME' 
; 
+0

正如@a_horse_with_no_name在另一個答案中所說的,一個同義詞不一定是表格。因此,如果同義詞指向例如第一個查詢可能會產生錯誤的否定結果。到一個視圖或程序._ – Krumia 2014-08-01 18:05:59

0
select distinct os.* 
from all_objects os 
     ,all_synonyms s 
where 1 = 1 
and os.object_type = 'SYNONYM' 
and os.STATUS  = 'INVALID' 
and os.object_name = s.synonym_name 
and not exists  (select unique(1) 
         from all_objects o1 
         where o1.object_name = s.TABLE_NAME 
         and o1.owner  = s.TABLE_OWNER) 
order by 2; 
+0

你應該給一些解釋 – 2014-10-13 05:54:20

相關問題