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 1elgAa-0001JQ-2I for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 19:23:12 +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 1elgAY-0002zi-T3 for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 19:23:10 +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 1elgAY-0002zX-Lg for pgsql-general@lists.postgresql.org; Tue, 13 Feb 2018 19:23:10 +0000 Received: from [66.111.4.29] (helo=out5-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1elgAW-0004no-1x for pgsql-general@postgresql.org; Tue, 13 Feb 2018 19:23:10 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 513F4213DB; Tue, 13 Feb 2018 14:22:46 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Tue, 13 Feb 2018 14:22:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=rgfbH7at0fuO8xxn2k/eyghxAVI1j rvDF58LqynSApI=; b=oU+Yut2JL9w7AiYQeijUCxKbLA+mbgG16H8rcjlnWvCD/ qTu42CESRi8sCRXEB9xYE2U0Wr2uCXIGRp+Uw7qjD5V6/H9xNwKYejv5hxw7Zrbq aqVVD8nNz6IlWUq6YZGmtNjHbTwNfeHdSoTHaiQdCaRX6O+gMdnLlWqtohXrFY6o bdFx4CCbrWB8HfiPFXApFFKjbo4UC/7fszcb0gVctptRVAHeiXJF13H1qPLTRNSe WLpNz5YKyYadX1KR0wL8OYWwH96fqCBPG3Lo6vBArM3NbnJEuLDS4fj9VpzmimFu j4B9NEi/oBmQNTVBsqjM3na1P7sw4tOE/RwZhgklA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=rgfbH7 at0fuO8xxn2k/eyghxAVI1jrvDF58LqynSApI=; b=ntUKkWMA+CEOH4X5VJ/7tv VKYGGf8aMROo8/d2GaH28TCEkgNKYZR4uNXzArFHbnAQo+Gt9b+G2MBKEixXy0EU nVo6jNtVZHInPO+k3w/rmdOgd2vbNb1zOGc1z5jhzUJbYFqAqMnwOrNFSEASNQgB CuC35V1vJkEzB7JnUjGf1ffD+Zy4O0ES+jwcEFJ2VZMzhj+4s8VhV1qzmJ0itVaw D/dwpmMg/wCaQzM1G/xSEDkrzPRou2ZbqHLtahnY1guGbJY7W0/beFie74theT7G DD1cjMIQDL+mhX1dTDMOKsY8uYiOKowQUM1nxttufEe/XbNC47pZ21QWhrWbAs3g == X-ME-Sender: Received: from [192.168.1.3] (71-212-36-41.tukw.qwest.net [71.212.36.41]) by mail.messagingengine.com (Postfix) with ESMTPA id C5D9F246AC; Tue, 13 Feb 2018 14:22:45 -0500 (EST) Subject: Re: cursors and function question To: armand pirvu Cc: "pgsql-general@postgresql.org" References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> <83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com> From: Adrian Klaver Message-ID: Date: Tue, 13 Feb 2018 11:22:44 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.29 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/13/2018 11:17 AM, armand pirvu wrote: > >> On Feb 13, 2018, at 12:54 PM, Adrian Klaver > > 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=> 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(); >>>             foofunc >>> ------------------------------- >>>  ("E1        ","CAT1      ",0) >>> But I am looking to get >>>             foofunc >>> ------------------------------- >>>  ("E1        ","CAT1      ",0) >>>  ("E1        ","CATs      ",0) >>> etc >>> Many thanks >>> — 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=1 >     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 Not following, are you looking to do this in an UPDATE trigger or somewhere else? Another way to ask is why do you want to use a cursor? > > 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 > > > > > > > > > -- Adrian Klaver adrian.klaver@aklaver.com