Subject | Re: [firebird-support] How to select all child records of a given parent in a self referencing table |
---|---|
Author | Anderson Farias |
Post date | 2009-06-25T16:37:22Z |
Hi,
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
>Does anyone got a query or a stored proc which will return all children ofyou may use a stored proc like this:
> 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.
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