Skip to Main Content
  • Questions
  • how to find the dependencies of table

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Ernesto.

Asked: February 21, 2007 - 2:59 pm UTC

Last updated: November 19, 2012 - 8:50 am UTC

Version: 9.2.0 10.1.0

Viewed 50K+ times! This question is

You Asked

Hi Tom !!!!

How to find the dependencies of table, if one procedure received the values of tables dynamically.

For example:

1 CREATE TABLE testtab (
2* testcol VARCHAR2(20))
SQL> /

Table created.

Elapsed: 00:00:00.04
SQL>
1 CREATE or replace PROCEDURE testproc IS
2 i PLS_INTEGER;
3 BEGIN
4 SELECT COUNT(*)
5 INTO i
6 FROM testtab;
7 dbms_output.put_line(TO_CHAR(i));
8* END testproc;
9 /

Procedure created.

Elapsed: 00:00:00.08

SQL> exec dbms_utility.get_dependency('TABLE', 'DWHADMIN','TESTTAB');
-
DEPENDENCIES ON DWHADMIN.TESTTAB
------------------------------------------------------------------
*TABLE DWHADMIN.TESTTAB()
* PROCEDURE DWHADMIN.TESTPROC() <---- show procedure

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.04/
SQL>
1 CREATE or replace PROCEDURE testproc IS
2 i PLS_INTEGER;
3 var_table varchar2(7):='TESTTAB'; <--- name of table
4 BEGIN
5 execute immediate 'SELECT COUNT(*) FROM testtab' INTO i;
6 dbms_output.put_line(TO_CHAR(i));
7* END testproc;
8 /

Procedure created.

Elapsed: 00:00:00.07

SQL> exec dbms_utility.get_dependency('TABLE', 'DWHADMIN','TESTTAB');
-
DEPENDENCIES ON DWHADMIN.TESTTAB
------------------------------------------------------------------
*TABLE DWHADMIN.TESTTAB()
<--- nothing


PL/SQL procedure successfully completed.

Elapsed: 00:00:00.03



I need locate this procedure ( because this procedure use the table TESTTAB ).

Exist any other solution?

Thanks in advance !!!


and Tom said...

there is no dependency here - think about it, there are just strings, strings whose values are set at runtime.

There is no dependency whatsoever between your table and this procedure, the are completely independent of each other.

This is why dynamic sql in PLSQL should be the path of last recourse. You should NEVER use dynamic sql unless and until there is no other choice.

If your procedure testproc is writing a variety of dynamic queries all on the one table, you could:

.... testproc
as
   cursor dummy is select * from testtbl;
   ....


by adding that dummy reference, you would set up a hard dependency - but it would be for reporting purposes only - it would not really "fix" anything in the code if you change the structure of testtbl.


Rating

  (7 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

find the dependencies of table

Ernesto Villarruel, February 21, 2007 - 5:51 pm UTC

Excellent ! ! ! ! !
Very very thanks !!!

SQL>
1 CREATE or replace PROCEDURE testproc IS
2 i PLS_INTEGER;
3 var_table varchar2(7):='TESTTAB';
4 cursor dummy is select * from TESTTAB;
5 BEGIN
6 execute immediate 'SELECT COUNT(*) FROM '||var_table||'' INTO i;
7 dbms_output.put_line(TO_CHAR(i));
8* END testproc;
9 /

Procedure created.

Elapsed: 00:00:00.09
SQL> exec dbms_utility.get_dependency('TABLE', 'DWHADMIN','TESTTAB');
-
DEPENDENCIES ON DWHADMIN.TESTTAB
------------------------------------------------------------------
*TABLE DWHADMIN.TESTTAB()
* PROCEDURE DWHADMIN.TESTPROC()

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.05
SQL>

This is my purpose.

I want development one procedure to get all names of procedures that affect a table.

The result should be:

TABLE PROCEDURE
testproc proc_1 <-- Procedure
testproc proc_2 <-- Procedure
testproc func_1 <-- Function
testproc pkg_load_1 <-- Package


So, i would set up a hard dependency in all procedures,functions,packages with contain sql dynamic??

Thanks again.

Tom Kyte
February 21, 2007 - 7:39 pm UTC

but do you see the problem here.....

I would presume that var_table would vary over time...

think about it.

Reverse engineering object dependencies for a new build

ht, March 12, 2007 - 5:54 pm UTC

Hi Tom,
During development, objects are compiled into the system using text files with "create <object>" syntax.

I would like to create a script that would order all of the objects in a schema so I can create a build script to rebuild into another schema.

I've pieced together this query:

select
lpad('.',1*(level-1),'.')||lower(name)||' --'||lower(type) objectname,level,
referenced_name,referenced_type*/
from user_dependencies
where/
referenced_name not in ('DBMS_OUTPUT','DUAL','STANDARD','HTP','SYS_STUB_FOR_PURITY_ANALYSIS',
'UTL_FILE','UTL_SMTP')
and referenced_type not in ('SEQUENCE','TABLE','NON-EXISTENT')
start with rownum=1
connect by nocycle prior name=referenced_name

and used dbms_utility.get_dependency but both solutions would involve multiple calls to an object that was already built.

Do you have a solution that would allow me to reverse-engineer all objects in a schema that I could use to create a concise build script?

Thanks,
ht
Tom Kyte
March 12, 2007 - 7:41 pm UTC

export....

export would do it.

besides export?

ht, March 12, 2007 - 8:33 pm UTC

Hi Tom,
I would like to reverse-engineer the build order into the build scripts I maintain and are checked into source control.

Is there a method that allows me to list the objects in order that they have to be built without building the same object multiple times? The 2 methods I described are showing multiple entries for each object so the result would be a longer-than-needed build process.

An alternative to expdp?

ht, March 13, 2007 - 12:53 pm UTC

Tom,
I attempted an expdp using "content=metadata_only include=PROCEDURE,VIEW" thinking that may give me what I need. Since there's a specific order in which the objects need to be built, it doesn't seem like this technique will work for me. Even if it did, is there a data dictionary query or package that will let me simply list all of a schema's objects in build sequence?

In other words, I would just like to generate a list of objects that I can then use with case statements to create a file with:
@obj1
@obj2
@obj3

Thanks.
ht

Help with a build script.

ht, March 21, 2007 - 5:38 pm UTC

Hi Tom,
I've attached an example of the issue I'm trying to solve. By using the scott schema, I created 4 views and 3 stored procedures. With the help of the user_dependencies data dictionary view, I can display a hierarchy of the objects.

I would like to create a build script that creates the objects in order but am having problems with duplicate values.

Since my schema has a few thousand objects, I would like to eliminate the duplicate values and be able to run the build script once, in order, avoiding the need to run multiple builds. I can't use export/import because I'm maintaining source control on the build files and need to build using that reference.

Thanks in advance.


Connected.

drop view empv1;

View dropped.

drop view empv2;

View dropped.

drop view empv3;

View dropped.

drop view empv4;

View dropped.


create view empv1 as select * from emp;

View created.


create view empv2 as select * from empv1;

View created.


create view empv3 as select * from empv2;

View created.


create view empv4 as select * from empv2;

View created.


create or replace procedure p1
2 is
3 v1 empv3.empno%type;
4 begin
5 select empno into v1 from empv3 where rownum=1;
6 end;
7 /

Procedure created.


create or replace procedure p2
2 is
3 v1 emp.empno%type;
4 begin
5 select empno into v1 from emp where rownum=1;
6 end;
7 /

Procedure created.


create or replace procedure p3
2 is
3 v1 empv4.empno%type;
4 begin
5 select empno into v1 from empv4 where rownum=1;
6 end;
7 /

Procedure created.


select
2 lpad('.',7*(level-1),'.')||ud.name name
3 from
4 user_dependencies ud
5 start with rownum=1
6 connect by nocycle
7 prior trim(ud.name)=trim(ud.referenced_name)
8 and prior trim(ud.type)=trim(ud.referenced_type);

P2
EMPV1
.......EMPV2
..............EMPV4
.....................P3
..............EMPV3
.....................P1
EMPV2
.......EMPV4
..............P3
.......EMPV3
..............P1
EMPV4
.......P3
EMPV3
.......P1
P1
P3
P3
P1
P2
P3
P1
P2

24 rows selected.


select
2 (
3 case
4 when type='VIEW' then 'sqlplus @../views/'||ud.name||'.sql'
5 when type='PROCEDURE' then 'sqlplus @../procs/'||ud.name||'
.sql'
6 end
7 )buildscript
8 from
9 user_dependencies ud
10 start with rownum=1
11 connect by nocycle
12 prior trim(ud.name)=trim(ud.referenced_name)
13 and prior trim(ud.type)=trim(ud.referenced_type);

sqlplus @../procs/P2.sql
sqlplus @../views/EMPV1.sql
sqlplus @../views/EMPV2.sql
sqlplus @../views/EMPV4.sql
sqlplus @../procs/P3.sql
sqlplus @../views/EMPV3.sql
sqlplus @../procs/P1.sql
sqlplus @../views/EMPV2.sql
sqlplus @../views/EMPV4.sql
sqlplus @../procs/P3.sql
sqlplus @../views/EMPV3.sql
sqlplus @../procs/P1.sql
sqlplus @../views/EMPV4.sql
sqlplus @../procs/P3.sql
sqlplus @../views/EMPV3.sql
sqlplus @../procs/P1.sql
sqlplus @../procs/P1.sql
sqlplus @../procs/P3.sql
sqlplus @../procs/P3.sql
sqlplus @../procs/P1.sql
sqlplus @../procs/P2.sql
sqlplus @../procs/P3.sql
sqlplus @../procs/P1.sql
sqlplus @../procs/P2.sql

24 rows selected.


How to find and use these dependencies?

Loïc, May 10, 2007 - 5:26 pm UTC

Hello Tom,
Often, developers come and ask me if it is possible to extract some rows of data from our production database in order to correct defect. Usually my answer is: I can't do that because of the numerous table dependencies. Say I want one row of the most used table and I have to retrieve rows from about 200+ tables to get the complete graph for this specific row or in other words to respect data integrity. Of course at the end, we have now a *full* copy of our production database so that developers (about 60) can re-copy this database (somewhat filtered) on their personal schema ending in development databases bigger than our production one.

My question is quite simple, is there a tool (SQL scripts, software...) that can achieve this goal? i.e. find dependencies (including cyclic) and extract all the data in a schema or a SQL file (containing inserts).

Thanks a lot in advance,
Loïc

Concerning Enhanced Finer Grained Dependency Management in Oracle Database 11g Release 1

Denis Pavlov, November 14, 2012 - 10:15 pm UTC

Sir, could You kindly clarify that topic in respect of the "Enhanced Finer Grained Dependency Management in Oracle Database 11g Release 1" (and 11gR2).

I know that Oracle 11gR1 manages table dependencies for schema objects on column level. So that in case of "independent" column modification another objects (that do not depend on the modified column) stay valid.
But I cannot obtain column-level dependency data from dba_dependencies.
To my regret, I also was unable to find an answer for my question in the Documentation.
Please, explain the way to get the field-level dependency.

Tom Kyte
November 19, 2012 - 8:50 am UTC

it isn't exposed in the dictionary. Just table level stuff is.


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library