craig
craig

Reputation: 26262

Recursively list concents of Oracle's DBA_DEPENDENCIES view

I would like a list of dependent tables (ultimately) of a given view.

For example:

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

The results:

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

I would like it to resemble:

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

I suppose that I should also have a column that lists the starting point, so I can keep the ancestry in a group.

I've tried the following query:

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

but I get an error that reads 'ORA-01436: CONNECT BY loop in user data'. What am I missing?

Upvotes: 3

Views: 21119

Answers (2)

Ollie
Ollie

Reputation: 17538

You want to specify the NOCYCLE keyword after your CONNECT BY:

i.e.

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

There is more info on NOCYCLE and the "CONNECT_BY_ISCYCLE" keywords here: http://www.dba-oracle.com/t_advanced_sql_connect_by_loop.htm

and here: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns001.htm

Hope it helps...

EDIT: After comments, you have missed the START WITH clause.

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, keeping the OWNER='FOO' where clause limits any dependencies returned to just FOO's object so you may possibly miss dependencies from other schemas.

Edit 2: The primary key of a table of view is owner, name thus the select should start with both and connect by both. You can use where to filter out desired results.

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;

Upvotes: 4

Rıdvan Korkmaz
Rıdvan Korkmaz

Reputation: 1

as Ollie said, This is as the same:
This query resolves all deps starting with MYPROC as root of the tree.

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

Upvotes: 0

Related Questions