Subject Re: [firebird-support] How to select all child records of a given parent in a self referencing table
Author Anderson Farias
Hi,


>Does anyone got a query or a stored proc which will return all children of
> a given parent in a self referencing table ?
> By children I mean, not only the immediate children, but also all children
> of
> the children and so on.

you may use a stored proc like this:

create procedure sp_tree (spid integer) returns (id integer, pid integer) as
begin
for select id, pid from tree where pid=:spid into :id, :pid do
begin
suspend;
for select id, pid from sp_tree(:id) into :id, :pid do
suspend;
end
end
^

than: select * from sp_tree(0);


*OR* on newer firebird versions, just use recursive CTEs


Regards,
Anderson Farias