Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elg5X-00013O-R1 for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 19:18:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1elg5W-0003Z7-ID for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 19:17:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1elg5W-0003YU-7W for pgsql-general@lists.postgresql.org; Tue, 13 Feb 2018 19:17:58 +0000 Received: from mail-it0-x22a.google.com ([2607:f8b0:4001:c0b::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1elg5S-0004gK-UZ for pgsql-general@postgresql.org; Tue, 13 Feb 2018 19:17:57 +0000 Received: by mail-it0-x22a.google.com with SMTP id n206so12120120itg.1 for ; Tue, 13 Feb 2018 11:17:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=42XLHCyqmsl8E0OO3LCtSPdytI8/GeWz/K5CalchqRA=; b=qZOda4QtkEY/Vaz2tnjxYu8OLWcUryr17++6/EdI08M1uKVa1NyubVaDFMTKuxJBui +11SQPOfbhVnvAcSQlsVQxf4AJrnR9XJIlrgDDgpPaHYWdDWZykViRSHR6HIiF1J8mCR U7ZAbmzcYGuZjS8ZbeyesSjQM0b2xJ1j18hhha4bTZrx4PLqt3i5UPlG4f610G9jYR2s +1I/OOR14RnZnI/hm5ucdByy5esYF9Wz0VD+mzj2Dx6dVMu3N3QRyx5QzVCw8SVmhU3W UCpr6Iuxh8nvbEkD6Mh4zXpEjt/jpJxua3KQsbrnXE4JVt1zpQivVZpnNfi3BCReSqlX UxEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=42XLHCyqmsl8E0OO3LCtSPdytI8/GeWz/K5CalchqRA=; b=VSNoTRh2VBGFuAUABf6gvOJLyTr5QMQV4S/HT+bOqt7VbVHcJ3HT0HQ/QX2B+iJIDg jrwskfmoizF2JIgMY1e7d63XnpmP92a0qWXTTwqQ5E9QAYnHLJ1va7GlTfNn6w+/BXw/ KfVKyjw/RkLe04nwF0TjJ0tz2CmW7Y2oX6fjV0j1gvQ1WKy6suQPWHGP/xhbov+nrNvA a++o5U/kPVvYCayTm/ZmizN9cz3b9ssEZgEFlIkfnDgAIDGD2QYEJxmZ9DsZkVC7fqZY LCYMwYx4G+9ubbu5GB1QcVM2tUKuWW6TjZQ32PABMZK8mwmXQZMqA8DU4zwKPveUZmu2 VF5Q== X-Gm-Message-State: APf1xPDjebTSHPcX9LuhLlbI5A6P6oxw9BtPjCnyFmCKASe3Dq0ORtL0 Fl93Sy7qqmEkWIwF2KKquZj8eORr X-Google-Smtp-Source: AH8x225owP+LzmO41EDRzNTaOYKIR3S+iRPP2OlaEtrcysCX9Dwvx3yyLCw/iKONxqEuOgmh3p/yJA== X-Received: by 10.36.246.70 with SMTP id u67mr2748147ith.29.1518549472341; Tue, 13 Feb 2018 11:17:52 -0800 (PST) Received: from ?IPv6:2605:a601:2062:d00:e97b:9c5a:f9a8:1f5? ([2605:a601:2062:d00:e97b:9c5a:f9a8:1f5]) by smtp.gmail.com with ESMTPSA id m7sm11566021ite.19.2018.02.13.11.17.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Feb 2018 11:17:51 -0800 (PST) From: armand pirvu Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_974F6A4F-FD99-47B5-A664-5B6C4C823C0E" Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: cursors and function question Date: Tue, 13 Feb 2018 13:17:14 -0600 In-Reply-To: <83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com> Cc: "pgsql-general@postgresql.org" To: Adrian Klaver References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> <83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com> X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_974F6A4F-FD99-47B5-A664-5B6C4C823C0E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Feb 13, 2018, at 12:54 PM, Adrian Klaver = wrote: >=20 > On 02/13/2018 10:22 AM, armand pirvu wrote: >> 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; >=20 >=20 > CREATE OR REPLACE FUNCTION public.foofunc() > RETURNS SETOF testtbl > LANGUAGE sql > AS $function$ > SELECT * FROM testtbl; > $function$ >=20 >=20 > test=3D> select * from foofunc(); > col1 | col2 | col3 > ------+------+------ > E1 | CAT1 | 0 > E1 | CAT2 | 0 > E1 | CAT3 | 0 > E4 | CAT1 | 0 > E5 | CAT1 | 0 > E6 | CAT1 | 0 > E7 | CAT1 | 0 > (7 rows) >=20 >=20 >> select foofunc(); >> foofunc >> ------------------------------- >> ("E1 ","CAT1 ",0) >> But I am looking to get >> foofunc >> ------------------------------- >> ("E1 ","CAT1 ",0) >> ("E1 ","CATs ",0) >> etc >> Many thanks >> =E2=80=94 Armand >=20 >=20 > --=20 > Adrian Klaver > adrian.klaver@aklaver.com Thanks Adrian That one I figured it out as well. The idea is that said table has some = records which I need to loop and do some processing using cursors = similar with=20 DECLARE cur CURSOR FOR SELECT * FROM testtbl FOR UPDATE; BEGIN FOR row IN cur LOOP UPDATE testtbl SET col3=3D1 WHERE CURRENT OF cur; END LOOP; return cur; END For a row update the goal is to return the cursor value be it = before/after the update, hence my question and test I found some code which seems to do what I need but it involves two = functions =20 CREATE or replace FUNCTION reffunc(refcursor) RETURNS refcursor AS $$ BEGIN OPEN $1 FOR SELECT col FROM test; RETURN $1; END; $$ LANGUAGE plpgsql; BEGIN; SELECT reffunc('funccursor'); FETCH ALL IN funccursor; COMMIT; And this is what beats me , aka can I put all in one / how ? Thanks Armand --Apple-Mail=_974F6A4F-FD99-47B5-A664-5B6C4C823C0E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On Feb 13, 2018, at 12:54 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

On 02/13/2018 10:22 = AM, armand pirvu wrote:
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;


CREATE OR REPLACE FUNCTION = public.foofunc()
RETURNS SETOF = testtbl
LANGUAGE sql
AS $function$
   SELECT * FROM testtbl;
$function$


test=3D> select * from = foofunc();
col1 | col2 | col3
------+------+------
E1   | CAT1 | =    0
E1   | = CAT2 |    0
E1   | = CAT3 |    0
E4   | = CAT1 |    0
E5   | = CAT1 |    0
E6   | = CAT1 |    0
E7   | = CAT1 |    0
(7 rows)


select foofunc();
          &nb= sp; foofunc
-------------------------------
 ("E1        ","CAT1 =      ",0)
But I am looking to = get
          &nb= sp; foofunc
-------------------------------
 ("E1        ","CAT1 =      ",0)
 ("E1 =        ","CATs =      ",0)
etc
Many = thanks
=E2=80=94 Armand


-- 
Adrian Klaver
adrian.klaver@aklaver.com



Thanks Adrian

That one I figured it out as well. The idea is = that said table has some records which I need to loop and do some = processing using cursors similar with 

DECLARE
cur CURSOR FOR SELECT = *
    FROM testtbl FOR = UPDATE;
BEGIN
  FOR row IN cur = LOOP
    UPDATE testtbl
    SET = col3=3D1
    WHERE CURRENT OF cur;
  = END LOOP;
  return cur;
END

For a row update the goal is to return the cursor = value  be it before/after the update, hence my question and = test

I found some code which seems = to do what I need but it involves two = functions
  
CREATE or replace = FUNCTION reffunc(refcursor) RETURNS refcursor AS = $$
BEGIN
    OPEN $1 FOR SELECT col FROM = test;
    RETURN $1;
END;
$$ = LANGUAGE plpgsql;

BEGIN;
SELECT = reffunc('funccursor');
FETCH ALL IN = funccursor;
COMMIT;


And this is what = beats  me , aka can I put all in one / how ?




Thanks
Armand









= --Apple-Mail=_974F6A4F-FD99-47B5-A664-5B6C4C823C0E--