Recursively list concents of Oracle's DBA_DEPENDENCIES view - oracle

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?

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;

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

Related

Oracle SQL developer- Create Table from Table and View

I have one table and view where one column is common which is the primary key of the table. Now if I want to join the table and view only with specific columns, should I create view or table in that case? Also I want to import the joined result to Tableau.
Well If you want just join table and view in single query you may write it, or you may create view for it, if you want. For example:
create table tmp_table_a (id, first_col, second_col, third_col) as
select level, lpad('a',level,'b'), lpad('c',level,'d'), lpad('e',level,'f')
from dual connect by level < 101;
create view v_tmp_a as
select id, substr(first_col,1,10) as first_sub_col from tmp_table_a;
simple query:
select second_col, third_col, first_sub_col
from tmp_table_a t1, v_tmp_a v1
where t1.id = v1.id;
or create view:
create view v_join_a as
select second_col, third_col, first_sub_col
from tmp_table_a t1, v_tmp_a v1
where t1.id = v1.id;
select * from v_join_a;

Select from multiple tables oracle

I'm new in Oracle. I have a table that lists tablenames of database. Its name is "AD_Table". I want to select ID table, and createdby from ad_table list. For example in ad_table it has one column name tablename that represents table name in database:
tablename
---------
AD_Tab1
AD_Tab2
AD_Tab3
AD_Tab4
AD_Tab5
AD_Tab6
AD_Tab7
AD_Tab8
AD_Tab9
AD_Tab10
I want query like this :
SELECT
createdby
from (SELECT TABLENAME FROM AD_TABLE)
but it won't work. Can anyone help?
In Oracle you can have many tables with the same name, on different schemas;
assuming that you need to find all the tables, and their owners, whose names are contained in your table, you can try with something like this:
select owner, table_name
from AD_table AD
inner join dba_tables DBA ON ( dba.table_name = UPPER(ad.tableName))
Notice that you need to log in with a user having rights to make a select on DBA_TABLES to run this query.

Find which oracle database object inserts data into a particular table?

I want to find the name of the stored procedure, package, function or trigger which is inserting data into a particular table, is there any inbuilt object so that i can use it. I am searching the name of the object from last 2 hours. Kindly help.
You cannot find the inserted PL/SQL block, but you can try out to identify the procedures/functions referring it. And increase your chances of finding.
select
proc_syn.nam,
proc_syn.referenced_owner,
proc_syn.referenced_name,
proc_syn.referenced_type,
syn_tab.table_name
from
dba_dependencies proc_syn, dba_synonyms syn_tab, dba_tables tables
where
REFERENCED_TYPE in ( 'SYNONYM','TABLE')
AND proc_syn.referenced_name = syn_tab.synonym_name
AND syn_tab.synonym_name = tables.table_name
AND syn_tab.owner = 'PUBLIC'
AND REFERENCED_NAME = 'YOUR_TABLE_NAME'
order by
proc_syn.referenced_owner, syn_tab.table_name;
The above query would return the table objects, that refer this table.
Note, this would return, when you use STATIC queries only. Any dynamic queries, is completely out of hand.
You can try by analyzing all the source of your DB with something like this:
select *
from dba_source
where upper(text) like '%TABLE_NAME%'
The pro of this approach is that you will get even dynamic code; the con is that you'll have even select, update, ...
If the table is statically referenced you can check the ALL_DEPENDENCIES view
SELECT * FROM ALL_DEPENDENCIES WHERE REFERENCED_NAME = '<your table>';
This will find all usages, not only insert
You can also search within source code:
SELECT * FROM ALL_SOURCE WHERE UPPER(TEXT) LIKE '%INSERT%<your table>%'
This will find the usage only if INSERT and your table are on the same line.
However if the command is built more dynamically then you will not find it that simply.
You can use user_source or user_dependencies table as below.
Using table user_source
select * from user_source
where upper(text) like '%<YOUR TABLE NAME in UPPER CASE>%'
You can select from user_source , all_source , dba_source depending on user you have logged in and permission that user have.
Using table user_dependencies
SELECT NAME FROM user_dependencies WHERE referenced_name = '<YOUR TABLE NAME in UPPER CASE>'
UNION
SELECT referenced_name FROM user_dependencies WHERE name = '<YOUR TABLE NAME in UPPER CASE>'
You can select from user_dependencies , all_dependencies , dba_dependencies depending on user you have logged in and permission that user have.

List of Oracle views using specific table name

I wish to find a list of all views under specific schema using mentioned table name.
e.g. if view1 and view2 uses table1, using table name "table1", I wish to find view names "view1" and view2".
Please let me know, how can I do it.
select
*
from
all_dependencies
where
type='VIEW'
and referenced_name like '%table_name%'
and referenced_type = 'TABLE'
Use this query:
SELECT *
FROM all_dependencies
WHERE TYPE = 'VIEW'
AND referenced_type = 'TABLE'

Oracle query that will list of the database objects referenced by a view

Is there an Oracle view that will list all of the database objects referenced by the DDL of a given view, procedure, or function?
So if a view is defined as:
CREATE OR REPLACE VIEW_B AS
SELECT * FROM TABLE_A INNER JOIN VIEW_A
running a query on this system view:
SELECT REF_OBJECT_NAME, REF_OBJECT_TYPE FROM V$XXX WHERE OBJECT_NAME='VIEW_B'
would list something like:
REF_OBJECT_NAME REF_OBJECT_TYPE
TABLE_A TABLE
VIEW_A VIEW
Try:
select * from dba_dependencies
where owner = 'view owner'
and name = 'view name';

Resources