日期:2014-05-16  浏览次数:20412 次

Oracle怎么查看表被数据库中那些object使用

????? 今天看到一个很有趣的问题,怎么查看一个table被数据库中那些object使用?原问题的链接如下:

????? http://www.itpub.net/forum.php?mod=viewthread&tid=1849997&extra=page%3D1%26filter%3Dtypeid%26typeid%3D37%26typeid%3D37

????? 出于好奇,我搜索了下,个人认为可以很好的回答上面的问题。

?

???? (一)正常情况(无动态SQL时)

????? 参考了链接:

???? http://www.oratable.com/which-objects-refer-to-this-table/

???? System/Sysdba用户,可以使用以下SQL查看:

????

select * from dba_dependencies  where referenced_name =upper('emp') and owner=upper('tmd')

??? 普通用户可以使用以下SQL查看:

???

select * from all_dependencies  where referenced_name =upper('emp') 

????上面的区别是可以看的用户范围不同:

????

DBA_DEPENDENCIES describes all dependencies between objects in the database. This view may only be accessible to users with DBA rights.
USER_DEPENDENCIES describes dependencies between objects only in the current user’s schema. This view does not have the column OWNER, as it is contained in the view definition.

??? 上面的运行结果如下:

????

???

?

???上面的缺点是如果表名在字符串里面,也就是存在动态执行sql的情况,则得不到对象名称。例子如下:

????

??

select * from all_dependencies  where  name like '%PROC_DBMS%' and  referenced_name =upper('emp') 

??? 结果如下:

???

?

?? (二)动态SQL情况

??? 对于动态sql只能从源码中查询了。参考了链接:

??? https://community.oracle.com/thread/517948

??? System/Sysdba可以使用下面的SQL查询:

????

select name,type,text from dba_source where upper(text) like '%EMP%'  and owner=upper('tmd')

??? 普通用户可以使用下面的SQL查询:

???

select name,type,text from user_source where upper(text) like '%EMP%' 

??? 以上面的例子为例:

???

select name,type,text from user_source where upper(text) like '%EMP%' and name like 'PROC_DBMS%'

??? 结果如下:

???

??

?? 查看所有引用表的动态sql:

?? System/Sysdba用户:

???

select name, type, text
  from dba_source
 where name in (select name
                  from dba_source