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 14BC63A52B2 for ; Mon, 22 Nov 2004 15:18:43 +0000 (GMT) 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 60241-01 for ; Mon, 22 Nov 2004 15:18:10 +0000 (GMT) Received: from www.bowmansystems.com (ns1.bowmansystems.com [65.166.193.2]) by svr1.postgresql.org (Postfix) with ESMTP id B5E453A529E for ; Mon, 22 Nov 2004 15:18:13 +0000 (GMT) Received: from ads2.ucomics.com (unknown [65.166.193.11]) by www.bowmansystems.com (Postfix) with ESMTP id DABF9CBC6 for ; Mon, 22 Nov 2004 09:07:21 -0600 (CST) Subject: Recursive SETOF function From: Richard Rowell To: pgsql-sql@postgresql.org Content-Type: text/plain Date: Mon, 22 Nov 2004 09:18:13 -0600 Message-Id: <1101136693.18148.66.camel@richard> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200411/231 X-Sequence-Number: 19550 I'm trying to port some TSQL to PLPGSQL. The DB has a table with a recursive foreign key that represents a tree hierarchy. I'm trying to re-create a TSQL function that pulls out all the ancestors of a given node in the hierarchy. I'm rather new to PLSQL and I have several questions. 1. In TSQL, I can assign a scalar to the result of query like so: SET @var1 = (SELECT foo FROM bar WHERE bar.uid=@var2) How would I do this in PLSQL? 2. In TSQL the "result table" can be inserted into manually. IE: CREATE FUNCTION foo () RETURNS @ttable TABLE( uid INTEGER) AS BEGIN INSERT @ttable VALUES (1) RETURN END Is there a way to manually insert rows into the result table in PLSQL? What follows is my TSQL function if that helps give context. CREATE FUNCTION svp_getparentproviderids (@child_provider INTEGER) RETURNS @provider_ids TABLE ( uid INTEGER ) AS BEGIN DECLARE @cid AS INTEGER IF (SELECT count(*) FROM providers WHERE uid =@child_provider) > 0 BEGIN SET @cid = @child_provider WHILE @cid IS NOT NULL BEGIN INSERT @provider_ids VALUES (@cid) SET @cid = (SELECT parent_id FROM providers WHERE uid=@cid) END END RETURN END --