pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: armand pirvu <armand.pirvu@gmail.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: cursors and function question
Date: Tue, 13 Feb 2018 12:22:19 -0600
Message-ID: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> (raw)

Hi 

Is there any elegant way not a two steps way I can output the cursor value at each step?


testtbl table has this content

    col1    |    col2    | col3 
------------+------------+------
 E1         | CAT1       |    0
 E1         | CAT2       |    0
 E1         | CAT3       |    0
 E4         | CAT1       |    0
 E5         | CAT1       |    0
 E6         | CAT1       |    0
 E7         | CAT1       |    0


This works
BEGIN WORK;
DECLARE fooc  CURSOR FOR SELECT * FROM testtbl;
FETCH ALL FROM fooc;
CLOSE fooc;
COMMIT WORK;

    col1    |    col2    | col3 
------------+------------+------
 E1         | CAT1       |    0
 E1         | CAT2       |    0
 E1         | CAT3       |    0
 E4         | CAT1       |    0
 E5         | CAT1       |    0
 E6         | CAT1       |    0
 E7         | CAT1       |    0


But 
CREATE OR REPLACE FUNCTION foofunc()
   RETURNS 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;


select foofunc();
            foofunc            
-------------------------------
 ("E1        ","CAT1      ",0)

But I am looking to get 

            foofunc            
-------------------------------
 ("E1        ","CAT1      ",0)
 ("E1        ","CATs      ",0)
etc 



Many thanks
— Armand



view thread (11+ messages)  latest in thread

Message-ID: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com>
Permalink:  ../149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com/
Also on:    postgresql.org/message-id/149EBC11-E33E-4DBA-9130-8C93BE4B1B43@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
  Subject: Re: cursors and function question
  In-Reply-To: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com>
  References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@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