我有表user_synonyms,在其中我可以看到名称,表和表所有者。有没有办法看看这个同义词是否仍然有效,e。如果引用表仍然存在但没有手动尝试?
答案 0 :(得分: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
。
如果要检查同义词是否为有效查询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
答案 1 :(得分: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'
;
答案 2 :(得分: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;