pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mike Rylander <mrylander@gmail.com>
To: Richard Rowell <richard@bowmansystems.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: [postgres] Re: Recursive SETOF function
Date: Mon, 22 Nov 2004 14:39:21 -0500
Message-ID: <b918cf3d0411221139505a15ce@mail.gmail.com> (raw)
In-Reply-To: <1101149501.18148.81.camel@richard>
References: <1101136693.18148.66.camel@richard>
	<b918cf3d041122085437cde2f1@mail.gmail.com>
	<b918cf3d04112208564c5fb783@mail.gmail.com>
	<1101149501.18148.81.camel@richard>

Sorry about that... try this:

CREATE OR REPLACE FUNCTION svp_getparentproviderids
(INTEGER)
  RETURNS SETOF svp_getparentproviderids_uid_type
  AS '
DECLARE
 child_provider ALIAS FOR $1;
 cid svp_getparentproviderids_uid_type%ROWTYPE;
 tmp_cid INTEGER;
BEGIN
   SELECT INTO tmp_cid count(*) FROM providers WHERE uid =child_provider;
   IF tmp_cid = 0 THEN
       RAISE EXCEPTION ''Inexistent ID --> %'', child_provider;
       RETURN;
   END IF;
   cid.uid := child_provider;
   LOOP
       EXIT WHEN tmp_cid IS NULL;
       RETURN NEXT cid;
       SELECT INTO tmp_cid parent_id FROM providers WHERE uid=cid.uid;
   END LOOP;
   RETURN;
END;' LANGUAGE 'plpgsql';

On Mon, 22 Nov 2004 12:51:41 -0600, Richard Rowell
<richard@bowmansystems.com> wrote:
> I have been fiddling with what you sent.  I have it working mostly, save
> for I keep getting syntax errors on the "RETURN NEXT cid;" line.  If I
> remove this line then the function works ( but returns nothing of
> course).  Any ideas?
> 
> sp_demo_505=# CREATE OR REPLACE FUNCTION svp_getparentproviderids
> (INTEGER)
>    RETURNS SETOF svp_getparentproviderids_uid_type
>    AS '
> DECLARE
>   child_provider ALIAS FOR $1;
>   cid INTEGER;
> BEGIN
>     SELECT INTO cid count(*) FROM providers WHERE uid =child_provider;
>     IF cid = 0 THEN
>         RAISE EXCEPTION ''Inexistent ID --> %'', child_provider;
>         RETURN;
>     END IF;
>     cid := child_provider;
>     LOOP
>         EXIT WHEN cid IS NULL;
>         RETURN NEXT cid;
>         SELECT INTO cid parent_id FROM providers WHERE uid=cid;
>     END LOOP;
>     RETURN;
> END;' LANGUAGE 'plpgsql';
> CREATE FUNCTION
> sp_demo_505=# select * from svp_getparentproviderids(21112);
> ERROR:  incorrect argument to RETURN NEXT at or near "cid"
> CONTEXT:  compile of PL/pgSQL function "svp_getparentproviderids" near
> line 13
> 
> 


-- 
Mike Rylander
mrylander@gmail.com
GPLS -- PINES Development
Database Developer



view thread (7+ messages)  latest in thread

Message-ID: <b918cf3d0411221139505a15ce@mail.gmail.com>
Permalink:  ../b918cf3d0411221139505a15ce@mail.gmail.com/
Also on:    postgresql.org/message-id/b918cf3d0411221139505a15ce@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mrylander@gmail.com, richard@bowmansystems.com
  Subject: Re: [postgres] Re: Recursive SETOF function
  In-Reply-To: <b918cf3d0411221139505a15ce@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox