递归列出Oracle DBA_DEPENDENCIES视图的内容

时间:2011-10-17 18:27:16

标签: oracle oracle10g recursive-query

我想要一个给定视图的依赖表(最终)列表。

例如:

SELECT NAME, TYPE, REFERENCED_NAME, REFERENCED_TYPE
FROM DBA_DEPENDENCIES
WHERE OWNER='FOO'
AND NAME='VIEW_O1'

结果:

VIEW_O1 VIEW    TABLE_01    TABLE
VIEW_O1 VIEW    TABLE_02    TABLE
VIEW_O1 VIEW    TABLE_03    TABLE
VIEW_O1 VIEW    VIEW_02     VIEW
VIEW_O1 VIEW    VIEW_03     VIEW

我希望它类似于:

VIEW_O1 VIEW    TABLE_01    TABLE
VIEW_O1 VIEW    TABLE_02    TABLE
VIEW_O1 VIEW    TABLE_03    TABLE
VIEW_O1 VIEW    VIEW_02     VIEW
VIEW_O1 VIEW    VIEW_03     VIEW
VIEW_O2 VIEW    TABLE_03    TABLE
VIEW_O2 VIEW    TABLE_04    TABLE
VIEW_O3 VIEW    TABLE_05    TABLE
VIEW_O3 VIEW    VIEW_04     VIEW
VIEW_O4 VIEW    TABLE_06    TABLE
VIEW_O4 VIEW    TABLE_07    TABLE
VIEW_O4 VIEW    TABLE_08    TABLE

我想我也应该有一个列出起点的专栏,所以我可以将祖先保留在一个小组中。

我尝试过以下查询:

SELECT NAME, TYPE, REFERENCED_NAME, REFERENCED_TYPE
FROM DBA_DEPENDENCIES
WHERE OWNER='FOO'
AND NAME='VIEW_01'
CONNECT BY PRIOR REFERENCED_NAME=NAME

但是我收到的错误是“用户数据中的ORA-01436:CONNECT BY循环”。我错过了什么?

2 个答案:

答案 0 :(得分:3)

您想在CONNECT BY之后指定NOCYCLE关键字:

SELECT NAME, 
       TYPE, 
       REFERENCED_NAME, 
       REFERENCED_TYPE 
  FROM DBA_DEPENDENCIES 
 WHERE OWNER='FOO' 
   AND NAME='VIEW_01' 
CONNECT BY NOCYCLE
  PRIOR REFERENCED_NAME = NAME;

此处有关于NOCYCLE和“CONNECT_BY_ISCYCLE”关键字的更多信息: http://www.dba-oracle.com/t_advanced_sql_connect_by_loop.htm

在这里: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns001.htm

希望它有所帮助...

编辑:评论后,您错过了START WITH子句。

SELECT NAME, 
       TYPE, 
       REFERENCED_NAME, 
       REFERENCED_TYPE 
  FROM DBA_DEPENDENCIES 
 WHERE OWNER='FOO' 
 START WITH NAME='VIEW_01' 
CONNECT BY NOCYCLE
  PRIOR REFERENCED_NAME = NAME;

BTW,保持OWNER ='FOO'where子句限制任何返回到FOO对象的依赖项,这样你可能会错过其他模式的依赖项。

编辑2: 视图表的主键是owner,name因此select应该从两者开始并由两者连接。您可以使用where过滤掉所需的结果。

SELECT OWNER, NAME, TYPE,  
   REFERENCED_OWNER,
   REFERENCED_NAME, 
   REFERENCED_TYPE 
FROM DBA_DEPENDENCIES 
-- where referenced_type='TABLE'
START WITH owner = 'FOO' AND NAME='VIEW_01' 
CONNECT BY NOCYCLE
   PRIOR REFERENCED_NAME = NAME
   AND PRIOR REFERENCED_OWNER = OWNER;

答案 1 :(得分:0)

奥利说, 这是一样的:
此查询将以 MYPROC 开头的所有deps解析为树的根目录。

SELECT A.NAME,
   A.TYPE,
   A.REFERENCED_OWNER,
   A.REFERENCED_NAME,
   A.REFERENCED_TYPE,
   A.REFERENCED_LINK_NAME,
   A.SCHEMAID  FROM USER_DEPENDENCIES A
 CONNECT BY PRIOR REFERENCED_NAME = A.NAME
 START WITH A.NAME = 'MYPROC'ORDER BY 1, 2, 3, 4