How to get Oracle Database dependents objects
Autor: Eduardo Valentim | Publicado: 2017-06-09T17:05:00-03:00

Hi folks, (português mais abaixo)

After some time I decided to continue posting at this blog even having a channel on youtube. My channel was created to share some stuff like tips, experiences, tutorials about Databases (specially Oracle), Linux and new techologies all in portuguese language.
If you are interested in this stuff, please go to https://www.youtube.com/channel/UCDncbo0hpBTEYMjZNZwsfmw and subscribe.&nbsp;

Today, I'm glad to share with you a query to get objects dependencies in Oracle Databases. It's very useful when you have to change some object and want to see if some others objects have to be updated too to avoid a broken application. Here I'm using a hierarchical query to get object depedency by level:&nbsp;

SELECT LPAD('-',level) || owner || '.' || name ||'('|| type || ')' DEPENDENCY
FROM dba_dependencies&nbsp;
CONNECT BY PRIOR owner = referenced_owner AND prior name = referenced_name&nbsp;
AND prior type = referenced_type&nbsp;
START WITH referenced_owner = '&amp;owner'
AND referenced_name = '&amp;object'
AND owner IS NOT NULL;

You have to provide OWNER and OBJECT you are going to change. The depedencies will be tree-like displayed as below:

SYS@TEST &gt; / &nbsp;&nbsp;
Enter value for owner: HR
Enter value for object: JOB_HISTORY

-HR.ADD_JOB_HISTORY(PROCEDURE)
&nbsp;-HR.UPDATE_JOB_HISTORY(TRIGGER)

2 rows selected.

Output above means UPDATE_JOB_HISTORY trigger depends on ADD_JOB_HISTORY procedure that depends on JOB_HISTORY table (provided object). Let's check:

SYS@TEST &gt; select text from dba_source where name='UPDATE_JOB_HISTORY';

TRIGGER update_job_history
&nbsp; AFTER UPDATE OF job_id, department_id ON employees
&nbsp; FOR EACH ROW
BEGIN
&nbsp; add_job_history(:old.employee_id, :old.hire_date, sysdate,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :old.job_id, :old.department_id);
END;

7 rows selected.

SYS@TEST &gt; select text from dba_source where name='ADD_JOB_HISTORY';

PROCEDURE add_job_history
&nbsp; ( &nbsp;p_emp_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_history.employee_id%type
&nbsp; &nbsp;, p_start_date &nbsp; &nbsp; &nbsp;job_history.start_date%type
&nbsp; &nbsp;, p_end_date &nbsp; &nbsp; &nbsp; &nbsp;job_history.end_date%type
&nbsp; &nbsp;, p_job_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_history.job_id%type
&nbsp; &nbsp;, p_department_id &nbsp; job_history.department_id%type
&nbsp; &nbsp;)
IS
BEGIN
&nbsp; INSERT INTO job_history (employee_id, start_date, end_date,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_id, department_id)
&nbsp; &nbsp; VALUES(p_emp_id, p_start_date, p_end_date, p_job_id, p_department_id);
END add_job_history;

13 rows selected.

I hope this helps you, as DBA or DEVELOPER, to avoid broke things up when changing objects.

Til next post.
Eduardo Valentim
-------------------------------------------------- Português --------------------------------------------------
Fala pessoal,

Depois de algum tempo decidi continuar postando nesse blog mesmo tendo um canal no youtube. Meu canal foi criado para compartilhar algumas coisas como dicas, experiências, tutoriais sobre bancos de dados (principalmente Oracle), Linux e novas tecnologias tudo em português. Se estiver interessado nesse tipo de coisa, por favor acesse&nbsp;https://www.youtube.com/channel/UCDncbo0hpBTEYMjZNZwsfmw&nbsp;e assine o canal.&nbsp;

Hoje estou feliz de trazer pra você uma consulta para obter as dependências de objetos no banco de dados Oracle. É muito útil quando você tem que alterar algum objeto e quer ver se outros objetos também precisam ser atualizados para evitar que a aplicação "quebre". Aqui estou usando uma consulta hierárquica para obter o nível de dependência dos objetos:&nbsp;

SELECT LPAD('-',level) || owner || '.' || name ||'('|| type || ')' DEPENDENCY
FROM dba_dependencies&nbsp;
CONNECT BY PRIOR owner = referenced_owner AND prior name = referenced_name&nbsp;
AND prior type = referenced_type&nbsp;
START WITH referenced_owner = '&amp;owner'
AND referenced_name = '&amp;object'
AND owner IS NOT NULL;

Você precisa fornecer o OWNER e o OBJECT que você modificará. As dependências aparecerão no estilo árvore conforme abaixo:

SYS@TEST &gt; / &nbsp;&nbsp;
Enter value for owner: HR
Enter value for object: JOB_HISTORY

-HR.ADD_JOB_HISTORY(PROCEDURE)
&nbsp;-HR.UPDATE_JOB_HISTORY(TRIGGER)

2 rows selected.

A saída acima significa que a trigger&nbsp;UPDATE_JOB_HISTORY depende da procedure ADD_JOB_HISTORY que depende da tabela JOB_HISTORY (objeto informado). Vamos verificar:

SYS@TEST &gt; select text from dba_source where name='UPDATE_JOB_HISTORY';

TRIGGER update_job_history
&nbsp; AFTER UPDATE OF job_id, department_id ON employees
&nbsp; FOR EACH ROW
BEGIN
&nbsp;&nbsp;add_job_history(:old.employee_id, :old.hire_date, sysdate,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :old.job_id, :old.department_id);
END;

7 rows selected.

SYS@TEST &gt; select text from dba_source where name='ADD_JOB_HISTORY';

PROCEDURE add_job_history
&nbsp; ( &nbsp;p_emp_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_history.employee_id%type
&nbsp; &nbsp;, p_start_date &nbsp; &nbsp; &nbsp;job_history.start_date%type
&nbsp; &nbsp;, p_end_date &nbsp; &nbsp; &nbsp; &nbsp;job_history.end_date%type
&nbsp; &nbsp;, p_job_id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_history.job_id%type
&nbsp; &nbsp;, p_department_id &nbsp; job_history.department_id%type
&nbsp; &nbsp;)
IS
BEGIN
&nbsp; INSERT INTO&nbsp;job_history&nbsp;(employee_id, start_date, end_date,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;job_id, department_id)
&nbsp; &nbsp; VALUES(p_emp_id, p_start_date, p_end_date, p_job_id, p_department_id);
END add_job_history;

13 rows selected.

Espero que ajude você, como DBA ou DESENVOLVEDOR, a evitar quebrar as coisas quando estiver alterando objetos!

Até o próximo post.
Eduardo Valentim.