Re: Recursive SETOF function

From: Mike Rylander <mrylander(at)gmail(dot)com>
To: Richard Rowell <richard(at)bowmansystems(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Recursive SETOF function
Date: 2004-11-22 16:56:31
Message-ID: b918cf3d04112208564c5fb783@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Forgot one line. See below

On Mon, 22 Nov 2004 11:54:30 -0500, Mike Rylander <mrylander(at)gmail(dot)com> 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 =(at)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=(at)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 =(at)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=(at)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(at)postgresql(dot)org so that your
> > message can get through to the mailing list cleanly
> >
>
>
> --
> Mike Rylander
> mrylander(at)gmail(dot)com
> GPLS -- PINES Development
> Database Developer
>

--
Mike Rylander
mrylander(at)gmail(dot)com
GPLS -- PINES Development
Database Developer

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Rowell 2004-11-22 19:25:52 Re: Recursive SETOF function
Previous Message Mike Rylander 2004-11-22 16:54:30 Re: Recursive SETOF function