X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 92E6752820 for ; Tue, 5 Jul 2005 03:29:12 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 10738-09 for ; Tue, 5 Jul 2005 06:29:06 +0000 (GMT) Received: from smtp.nildram.co.uk (smtp.nildram.co.uk [195.112.4.54]) by svr1.postgresql.org (Postfix) with ESMTP id E675F52816 for ; Tue, 5 Jul 2005 03:29:04 -0300 (ADT) Received: from HYDROGEN (unknown [62.3.216.34]) by smtp.nildram.co.uk (Postfix) with ESMTP id 8BB0C24DEF9; Tue, 5 Jul 2005 07:29:03 +0100 (BST) From: "Nick Stone" To: "'Gnanavel Shanmugam'" , Subject: Re: Recursive function Date: Tue, 5 Jul 2005 07:27:06 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.6353 In-Reply-To: Thread-Index: AcWBIDeAaFQiMVuEQ3G+Nl0xk2cxLQACinpQ X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Message-Id: <20050705062903.8BB0C24DEF9@smtp.nildram.co.uk> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200507/21 X-Sequence-Number: 21999 Hi, Also you could take a look at the connectby function as this will do what you want with ease and it can be used to sort stuff at the same time, produce tree views of data etc. Nick -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Gnanavel Shanmugam Sent: 05 July 2005 06:05 To: pgsql-sql@postgresql.org Subject: [SQL] Recursive function Hi, I have a table with the following details. section_type_id | section_type_name | parent_section_type_id -----------------+-------------------+------------------------ 10 | Unit | 20 | Block | 10 30 | Practice | 20 40 | Sub Practice | 30 I've written a function as this CREATE or replace FUNCTION get_child_section_types(int) RETURNS setof int AS $$ declare v_section_type_id alias for $1; v_rec record; begin for v_rec in select section_type_id from master_section_type where parent_section_type_id=v_section_type_id loop return next v_rec.section_type_id; end loop; return; end; $$ LANGUAGE plpgsql; which returns output like, select * from get_child_section_types(10); get_child_section_types ------------------------- 20 but I need the function to return all section types under the child nodes too. So, how to make the function to be recursive. with thanks & regards, S.Gnanavel ---------------------------(end of broadcast)--------------------------- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match