Subject RE: [firebird-support] Sample stored procedure of recursive query for FB1.5
Author Sasha Matijasic
> Hi All,
>
> I know that CTE and WITH RECURSIVE CLAUSE are supported in FB20, 21.
> But I have to do in FB15.
>
> Is there any sample code (or URL) for reqursive query using stored
> procedures of FB15?
>

Hi, here is some sample code.

CREATE TABLE FOLDERS(
FOLDER_ID Integer NOT NULL,
PARENT_FOLDER_ID Integer,
FOLDER_NAME Varchar(100) NOT NULL,
CONSTRAINT PK_FOLDERS PRIMARY KEY (FOLDER_ID)
);

SET TERM ^ ;
CREATE PROCEDURE SUBFOLDERS (FOLDER_ID Integer )
RETURNS (SUBFOLDER_ID Integer )
AS
declare variable temp_folder_id integer;
begin
if (not exists(select null from folders where folder_id = :folder_id)) then
exit;
subfolder_id = :folder_id;
suspend;
for select folder_id from folders where parent_folder_id = :folder_id into :temp_folder_id do
begin
for select subfolder_id from subfolders(:temp_folder_id) into :subfolder_id do
suspend;
end
end^
SET TERM ; ^

It's something I played around with some time ago, may be errors in it but it should get you going in the right direction. It was developed on 2.0, but it should run on 1.5 also.
Feel free to ask if you need more help.

Sasha