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

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';

Related

Where is this Oracle object?

I have some SQL scripts; one of them references an object called "names". I can:
select * from names
and it returns results. However, I cannot see a table called "names". Neither can I see a view called "names". I can't find a custom type called "names".
If I look for one of the columns that is returned by the query select * from names using:
select * from sys.all_tab_columns where column_name like '%MyColumn%'
it finds a table called LSNAMES, but that has no rows.
Any ideas how I can find this "table"?
I'd start the research with understanding what type of object this is:
SELECT owner, object_name, object_type
FROM all_objects
WHERE object_name = 'NAMES'
Once you have a type, you could query some more information from all_<type>s
Look for:
A materialized view called NAMES
SELECT * FROM ALL_MVIEWS WHERE MVIEW_NAME = 'NAMES';
A synonym called NAMES
SELECT * FROM ALL_SYNONYMS WHERE SYNONYM_NAME = 'NAMES';

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;

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.

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?
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

Oracle: Is there a way to get the column data types for a view?

For a table in oracle, I can query "all_tab_columns" and get table column information, like the data type, precision, whether or not the column is nullable.
In SQL Developer or TOAD, you can click on a view in the GUI and it will spit out a list of the columns that the view returns and the same set of data (data type, precision, nullable, etc).
So my question is, is there a way to query this column definition for a view, the way you can for a table? How do the GUI tools do it?
You can use user_tab_columns (or all_tab_columns and dba_tab_columns respectively) regardless if table_name refers to a view or a table.
View columns appear in all_tab_columns, so you can query them just as you can tables.
Just simply write this query:
SQL> desc TABLE/VIEW NAME;
For example if the table/view name is "department" Then just write:
SQL> desc department;
This will give the list of all fields, it's type and default Null info of the table or view.
you can use the ANSI catalog views, should work for most RDBMs
select *
from information_schema.columns c
join information_schema.tables t on c.table_name = t.table_name
where table_type = 'view'

Resources