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 A63FF3A52D7 for ; Mon, 22 Nov 2004 16:57:00 +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 96006-01 for ; Mon, 22 Nov 2004 16:56:34 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.197]) by svr1.postgresql.org (Postfix) with ESMTP id 2C0EA3A3D32 for ; Mon, 22 Nov 2004 16:56:32 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id a36so221297rnf for ; Mon, 22 Nov 2004 08:56:32 -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=GmkqlWesFqTKgZJeVsVVHPgi/uAzNVcjSYlY1KoG5KJ/7KeSvehKZpg3lm4V8MmTMG++3N9b7bjHiw7NmG3OXYRTk9lyY9hRTVOEt74Gt96YD1unHB12cpc4hJ6w8oIdhYbBNx8MUJ2hzsKh0UtAzkAgREE68oIFLdde+XA6I/o= Received: by 10.38.163.5 with SMTP id l5mr76442rne; Mon, 22 Nov 2004 08:56:31 -0800 (PST) Received: by 10.38.126.17 with HTTP; Mon, 22 Nov 2004 08:56:31 -0800 (PST) Message-ID: Date: Mon, 22 Nov 2004 11:56:31 -0500 From: Mike Rylander Reply-To: Mike Rylander To: Richard Rowell Subject: Re: Recursive SETOF function Cc: pgsql-sql@postgresql.org In-Reply-To: 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/234 X-Sequence-Number: 19553 Forgot one line. See below On Mon, 22 Nov 2004 11:54:30 -0500, Mike Rylander wrote: > I'm feeling sausey today, so here is my (untested) attempt to [snip] > > 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 * FROM providers WHERE uid =@child_provider) > 0 IF NOT FOUND RETURN; END IF; > 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 > -- Mike Rylander mrylander@gmail.com GPLS -- PINES Development Database Developer