Board index » delphi » Interbase[5] - get all descendents/ancestors (Parts List type problem)

Interbase[5] - get all descendents/ancestors (Parts List type problem)

This is the basic standard table of parts problem,
I know I need 2  recursive stored procs, but can't get them to work
correctly using Interbase 5 and I getting horribly frustrated.

I can get the immediate descendent and ancestor, but can't get further up or
down.

Given a  link table that encapsulates a many to many as follows
------           parent        ---------------
module -----------------<| heirarchy |
-------     -----------------<  ---------------
                child

I need to find all descendents of a module , and its ancestors
eg

if module is 1 and calls 2,3 and 3 calls 34,35,46 I would expect to see the
a result set of
Parent Child
1          2
1          3
3          34
3         35
3         46

and using the above example if 35 were also called by 6 and 6 was called 4 I
would expect the result set
Parent Child
6          35
6          4
3          35
3         1

TIA
Kim

 

Re:Interbase[5] - get all descendents/ancestors (Parts List type problem)


More on my question

This is the proc

set term ^ ;

alter procedure calltree  (x integer)
returns (parent integer,child integer, z  integer)
as
        declare variable cp integer;
        declare variable cc integer;
        declare variable cnt integer;
        declare variable y integer;
begin
   y = x;

   parent = x;

   select count(*) from heirarchy where parent = :x into :cnt;
   if (cnt = 0) then
   begin
    suspend;
   end

   for select distinct child from heirarchy where parent = :x into :child
   do
   begin
     execute procedure calltree :child returning_values :cp, :cc, :y;
     z = cc;
     suspend;
   end

  end^

These are the results
select * from calltree(1)

     PARENT       CHILD           Z
=========== =========== ===========

          1          14      <null>
          1          15          52
          1          16      <null>
          1          17      <null>
          1          18          48
          1          47      <null>
          1          48      <null>

from this I can see that the proc does call itself, but I can't get the
entry for 15 or 18 that calls 52 or 48  ie 115 52 <null and 8 48 <null>
what have I done wrong ???

TIA
Kim

Quote
Kim Mihaly wrote in message ...
>This is the basic standard table of parts problem,
>I know I need 2  recursive stored procs, but can't get them to work
>correctly using Interbase 5 and I getting horribly frustrated.

>I can get the immediate descendent and ancestor, but can't get further up
or
>down.

>Given a  link table that encapsulates a many to many as follows
>------           parent        ---------------
>module -----------------<| heirarchy |
>-------     -----------------<  ---------------
>                child

>I need to find all descendents of a module , and its ancestors
>eg

>if module is 1 and calls 2,3 and 3 calls 34,35,46 I would expect to see the
>a result set of
>Parent Child
>1          2
>1          3
>3          34
>3         35
>3         46

>and using the above example if 35 were also called by 6 and 6 was called 4
I
>would expect the result set
>Parent Child
>6          35
>6          4
>3          35
>3         1

>TIA
>Kim

Other Threads