como fazer esta Consulta hierarquica em sys.all_constraints?

Dúvidas, dicas e truques de PL/SQL. Aqui também vão assuntos relacionados a pacotes, triggers, funções, Java-Stored Procedures, etc
Responder
renatotn7
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Mensagens: 5
Registrado em: Ter, 30 Jun 2009 11:31 am
Localização: RJ

olá :-)

estou tentando fazer uma consulta hierarquica que me dê os nomes de tabelas que estão ligadas a uma tabela pai por constraints

o select que me da o nome da tabela filho é :

Selecionar tudo

select 
    filho.table_name                             -- table_name é a tabela filha
from 
    sys.all_constraints filho 
where 
    filho.constraint_type = 'R'                -- tipo R é fk
and 
    filho.owner = 'OWNERXX'
and 
   filhlo.r_constraint_name in                
 (
       select 
            constraint_name 
      from 
             sys.all_constraints pai
      where 
              pai.constraint_type in ('P', 'U')  --P é PK
      and 
              pai.table_name = 'NOME_DA_TABELA_RAIZ'
              pai.owner = 'OWNERXX'

 )
order by
      filho.table_name,
      filho.constraint_name 

eu estou tentando obter SYS_CONNECT_BY_PATH(table_name, '/')
que me daria o path de caminhamento separado por '/'
exemplo tabelaPai/tabelaFilho/tabelaNeto/...

infelizmente não estou conseguindo fazer uma consulta hierarquica funcional....

por favor...
me ajudem hehe
Avatar do usuário
fsitja
Rank: OraSauro
Rank: OraSauro
Mensagens: 611
Registrado em: Seg, 19 Jan 2009 4:29 pm
Localização: Gaúcho no Rio de Janeiro - RJ
"The scars exist to remind us that the past was real"
Campanha: Como fazer uma pergunta e obter uma resposta.
http://tkyte.blogspot.com/2005/06/how-t ... tions.html

OCA & OCP Developer — OCE SQL Expert — OCS Data Warehousing Specialist

Deve ter jeito mais simples ou pronto de fazer isso, mas como eu estava a fim de queimar uns neurônios fiz isso aqui pra brincar. Não testei muito, só para umas poucas tabelas, e já notei que tem alguns problemas, principalmente envolvendo ciclos e o level máximo que dá para descer... fica bem lento.

Selecionar tudo

select u.owner, u.table_name, u.constraint_name,
       sys_connect_by_path(u.table_name, '<=') "caminho",
       connect_by_root table_name "pai",
       u.fk_filha,
       level,
       connect_by_iscycle ciclo
  from (
  select upr.constraint_name as fk_filha,
         upr.r_constraint_name as fk_constraint_name,
         upr.r_owner as fk_owner,
         up.*
    from user_constraints up
    left join user_constraints upr on up.table_name = upr.table_name
                             and up.owner = upr.owner
                             and upr.constraint_type = 'R'
 where up.constraint_type in ('R', 'P', 'U')
 ) u
-- start with u.table_name = upper('TABELAXXX') -- para teste numa tabela pai
connect by nocycle
        u.fk_owner = prior u.owner
        and u.fk_constraint_name = prior u.constraint_name
        and level < 5
order siblings by u.table_name
Responder
  • Informação