I am attempting to write a query that will traverse the relationships
for tables in my database that will provide me the order in which I
need to perform delete operations to avoid running into foreign key
constraints. I have determined the delete order manually, but since the
database is complex and always having to structures added to it as the
scope of the application grows, I was looking for an automated
approach.
I tried modelling the query after some recursive examples from Graeme
Birchall's SQL Cookbook, but am missing something fundamental and the
query produces "infinite" output. I have been using SYSCAT.REFERENC ES
as my target table (view). The last incarnation of my query looks
something like this:
with temp1 (src_name, depends_on) as
((select root.reftabname , root.tabname
from syscat.referenc es root
where root.reftabname ='PX_CASE')
union all
( select child.reftabnam e, child.tabname
from temp1 parent, syscat.referenc es child
where parent.depends_ on = child.tabname)
)
select depends_on
from temp1
order by depends_on;
There are only 130 FK constraints on the collective tables, yet this
produces thousands of rows of output before I end up manually
terminating it. I have probably bitten off more than I can chew, but am
convinced that the solution exists to this problem. Can anyone make any
suggestions?
Thanks,
Mike