pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: armand pirvu <armand.pirvu@gmail.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: cursors and function question
Date: Tue, 13 Feb 2018 13:03:05 -0600
Message-ID: <4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@gmail.com> (raw)
In-Reply-To: <CAKFQuwaiygt-u=EgzwzSQwHAui_iyMYzWi_+nZSuTsmNhu99mA@mail.gmail.com>
References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com>
	<CAKFQuwaiygt-u=EgzwzSQwHAui_iyMYzWi_+nZSuTsmNhu99mA@mail.gmail.com>


> On Feb 13, 2018, at 12:26 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
> 
> On Tuesday, February 13, 2018, armand pirvu <armand.pirvu@gmail.com <mailto:armand.pirvu@gmail.com>> wrote:
> 
> CREATE OR REPLACE FUNCTION foofunc()
>    RETURNS text AS $$
> 
> select foofunc();
>             foofunc
> -------------------------------
>  ("E1        ","CAT1      ",0)
> 
> But I am looking to get
> 
>             foofunc
> -------------------------------
>  ("E1        ","CAT1      ",0)
>  ("E1        ","CATs      ",0)
> 
> 
> You need to specify SETOF
> 
> CREATE FUNCTION foofunc() RETURNS SETOF text AS
> 
> David J.

Thank you but


CREATE OR REPLACE FUNCTION foofunc()
   RETURNS setof text AS $$
DECLARE 
 var2   RECORD;
 cur  CURSOR FOR SELECT * from testtbl;
BEGIN
   OPEN cur;
    LOOP
      FETCH cur INTO var2;
         return  var2;
   END LOOP;
   CLOSE cur;
END; $$
LANGUAGE plpgsql;

ERROR:  RETURN cannot have a parameter in function returning set
LINE 10:          return  var2;
HINT:  Use RETURN NEXT or RETURN QUERY.


so I employed next



CREATE OR REPLACE FUNCTION foofunc()
   RETURNS setof  text AS $$
DECLARE 
 var2   text;
 cur  CURSOR FOR SELECT col1 from testtbl;
BEGIN
   OPEN cur;
    LOOP
      FETCH cur INTO var2;
         return next var2;
   END LOOP;
   CLOSE cur;
END; $$
LANGUAGE plpgsql;



and it just sits there

Any hints ?


Thank you
— Armand

view thread (11+ messages)  latest in thread

Message-ID: <4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@gmail.com>
Permalink:  ../4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@gmail.com/
Also on:    postgresql.org/message-id/4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: armand.pirvu@gmail.com, david.g.johnston@gmail.com
  Subject: Re: cursors and function question
  In-Reply-To: <4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@gmail.com>
  References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> <CAKFQuwaiygt-u=EgzwzSQwHAui_iyMYzWi_+nZSuTsmNhu99mA@mail.gmail.com> <4EC32AB4-3BCF-47F1-AEF2-9478B6F02C51@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