2

我有表 user_synonyms,我可以在其中看到名称、表和表所有者。有没有办法查看这个同义词是否仍然有效,即 如果没有手动尝试引用表仍然存在?

4

3 回答 3

5

您可以通过加入 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
于 2013-02-20T15:14:30.700 回答
2
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;
于 2014-10-13T05:27:14.753 回答
0

Use the below query:

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'
;
于 2014-08-01T18:01:07.830 回答