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 328FE3A527D for ; Mon, 22 Nov 2004 16:55:08 +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 93552-05 for ; Mon, 22 Nov 2004 16:54:32 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.204]) by svr1.postgresql.org (Postfix) with ESMTP id 6D46B3A52A7 for ; Mon, 22 Nov 2004 16:54:31 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id a36so221106rnf for ; Mon, 22 Nov 2004 08:54:30 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=aP7OCCxn2K4W5wWSNdNAvbuo1lNOWm1P8m/2EUDPCd7N6t6VCpcGVfssl4CMyz0xdHmi7LIaNI0UIfOy5lLPdy+AnKYsEd0dCv0PDN/F1P/jDMC6U2eDR4xWCdrIdrnf5hSQAmO4RdReVSj7GGDvaDehgNssc8kBwSdZExPEf48= Received: by 10.38.150.46 with SMTP id x46mr917384rnd; Mon, 22 Nov 2004 08:54:30 -0800 (PST) Received: by 10.38.126.17 with HTTP; Mon, 22 Nov 2004 08:54:30 -0800 (PST) Message-ID: Date: Mon, 22 Nov 2004 11:54:30 -0500 From: Mike Rylander Reply-To: Mike Rylander To: Richard Rowell Subject: Re: Recursive SETOF function Cc: pgsql-sql@postgresql.org In-Reply-To: <1101136693.18148.66.camel@richard> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <1101136693.18148.66.camel@richard> 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/233 X-Sequence-Number: 19552 I'm feeling sausey today, so here is my (untested) attempt to translate your function. It's inline below, and you'll want to look here http://www.postgresql.org/docs/7.4/interactive/plpgsql.html for more information. On Mon, 22 Nov 2004 09:18:13 -0600, Richard Rowell wrote: > 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 > -- This TYPE will get you a named column... easier to use SRFs with a preexisting type. CREATE TYPE svp_getparentproviderids_uid_type AS ( uid INTEGER ); CREATE FUNCTION svp_getparentproviderids (INTEGER) RETURNS SETOF svp_getparentproviderids_uid_type AS ' DECLARE child_provider ALIAS FOR $1; cid INTEGER; BEGIN SELECT count(*) FROM providers WHERE uid =@child_provider) > 0 LOOP cid := child_provider IF cid IS NULL THEN EXIT; END IF; RETURN NEXT cid; SELECT INTO cid parent_id FROM providers WHERE uid=@cid; END LOOP; RETURN END;' LANGUAGE 'plpgsql'; Hope that helps! > -- > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly > -- Mike Rylander mrylander@gmail.com GPLS -- PINES Development Database Developer