Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 12th, 2005, 06:26 AM
maricel
Guest
 
Posts: n/a
Default How to list tables in order (parent & child relationship)

Is there a way to list (using db2 command or catalogs) to list hierarchy of
table parent & child relationship:

1) A list that shows which table should be deleted first,second,third...
2) A list that shows which table should loaded first, second,third...

Your help would be highly appreciated.

maricel.


  #2  
Old November 12th, 2005, 06:26 AM
Serge Rielau
Guest
 
Posts: n/a
Default Re: How to list tables in order (parent & child relationship)

Yes, you can do that using the catalogs and a recursive common table
expression (CTE).
There are numerous examples on developerWorks.
To keep track of order you can mark the relationship.
google for: db2 node type
You will find an article by Jacques Roy that will be helpful

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
  #3  
Old November 12th, 2005, 06:26 AM
Sean C.
Guest
 
Posts: n/a
Default Re: How to list tables in order (parent & child relationship)

Try this:

with leaf_children (leaf_child) as
(select tabname from syscat.tables where tabschema = 'DB2ADMIN'
and parents > 0 and children = 0 ),

refs (parentschema, parent, childschema, child, levels) as
((
select reftabschema, reftabname, tabschema, tabname, 4
from syscat.references, leaf_children
where tabschema = 'DB2ADMIN'
and tabname = leaf_child)

union all

(select reftabschema, reftabname, tabschema, tabname, levels - 1
from refs r, syscat.references s
where s.tabschema = r.parentschema
and s.tabname = r.parent
and r.levels > 0))

select distinct substr(r.parent,1,20) as parent, '>>> ' ||
substr(r.child,1,20) as child, levels
from refs r, (select parent, min(levels) as level
from refs group by parent) as p
where r.parent = p.parent and r.levels = p.level
union
select substr(leaf_child,1,20), ' ', (select max(levels) from refs) +
1 from leaf_children
order by 3;

This recurses down the RI chain and gives an ordered list of parents
pointing to children. You need to set the number of levels you expect
(start with a pessimistically high number) by modifying the last
number on this line, above:

select reftabschema, reftabname, tabschema, tabname, 4

You'll also want to change the schema name in the query.

Hope that helps
Sean



"maricel" <maricel@xtra.co.nz> wrote in message news:<5aITb.32508$9k7.683461@news.xtra.co.nz>...[color=blue]
> Is there a way to list (using db2 command or catalogs) to list hierarchy of
> table parent & child relationship:
>
> 1) A list that shows which table should be deleted first,second,third...
> 2) A list that shows which table should loaded first, second,third...
>
> Your help would be highly appreciated.
>
> maricel.[/color]
  #4  
Old November 12th, 2005, 06:27 AM
Paul Reddin
Guest
 
Posts: n/a
Default Re: How to list tables in order (parent & child relationship)

Hi,

I have developed a view, if you would like a copy let me know.

DBA.TABLE_LOAD_SEQUENCE
- basically determines hierarchy of tables (recursively from
catalog) and displays them with a sequence number for loading (reverse
order for deleting)

Let me know if you would like a copy.

Paul.

"maricel" <maricel@xtra.co.nz> wrote in message news:<5aITb.32508$9k7.683461@news.xtra.co.nz>...[color=blue]
> Is there a way to list (using db2 command or catalogs) to list hierarchy of
> table parent & child relationship:
>
> 1) A list that shows which table should be deleted first,second,third...
> 2) A list that shows which table should loaded first, second,third...
>
> Your help would be highly appreciated.
>
> maricel.[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,335 network members.