Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a7OpQ-0002y3-Fg for pgsql-jdbc@arkaria.postgresql.org; Fri, 11 Dec 2015 14:37:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a7OpQ-0005xD-2M for pgsql-jdbc@arkaria.postgresql.org; Fri, 11 Dec 2015 14:37:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a7OpP-0005wq-CT for pgsql-jdbc@postgresql.org; Fri, 11 Dec 2015 14:37:47 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a7OpM-0006vG-D7 for pgsql-jdbc@postgresql.org; Fri, 11 Dec 2015 14:37:46 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 726CD22744 for ; Fri, 11 Dec 2015 09:37:43 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Fri, 11 Dec 2015 09:37:43 -0500 DKIM-Signature: v=1; a=rsa-sha1; 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-sasl-enc :x-sasl-enc; s=mesmtp; bh=HKK3CIuWFHNH0BK+gAEaM1AI3OM=; b=a/FAbf b7w6UZyu04LIwhp39wx8hHfn4BIoNSG58Hc89fQKyLeV/Ijl8kmiS04Df0uqqXiH o2WmwdaQO2FYnsiLFVSujg/YanUEHqGfuCMMfINbdWfbx5ZXlImhdy3dhsYqX8O0 5vRTNkVoPhb+eq7bnvTVhopHSJ/Hv7ORSEAX8= DKIM-Signature: v=1; a=rsa-sha1; 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-sasl-enc:x-sasl-enc; s=smtpout; bh=HKK3CIuWFHNH0BK +gAEaM1AI3OM=; b=YY8xcXLjMhE8Me/5N4jpf3Ex9OxHVAj9jv5c5Gg2sguJHmN IMAIYNcpY87rT3aEZgPNZfHOPexw2aiNrlvB5PqDf1WTRUjqx4sxclcsKO+bp/ix G/oeUpEvvK8nSoD7AKhG5E3pr12X7PzJZlXEOVabrD98AgPFsQqJtk6lwZGE= X-Sasl-enc: 9Jy+ZohOGqrsjxioMZsKtvLkYQ8bmZpHqITaXVZZKx6m 1449844663 Received: from [192.168.1.2] (65-102-182-223.tukw.qwest.net [65.102.182.223]) by mail.messagingengine.com (Postfix) with ESMTPA id 61C426800C7; Fri, 11 Dec 2015 09:37:42 -0500 (EST) Subject: Re: [GENERAL] plpgsql function with RETURNS SETOF refcursor in JAVA To: "Corradini, Carlos" , pgsql-jdbc@postgresql.org, pgsql-general@postgresql.org References: <2A8F1D1266E80A4C8E5DF89F3042279711936AFC@B1842ZACS0046.correo.local> <5669EDC8.9070805@aklaver.com> <2A8F1D1266E80A4C8E5DF89F304227971195D1A3@B1842ZACS0046.correo.local> Cc: books@ejurka.com From: Adrian Klaver Message-ID: <566ADF96.4000308@aklaver.com> Date: Fri, 11 Dec 2015 06:37:10 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <2A8F1D1266E80A4C8E5DF89F304227971195D1A3@B1842ZACS0046.correo.local> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org On 12/11/2015 04:56 AM, Corradini, Carlos wrote: > Mr. Adrian, here i transcribe the code of the function Notes in line. > > -- Function: dw_bsc.proc_perspectives(character varying, integer, > character varying, character varying, character varying, integer, date) > > -- DROP FUNCTION dw_bsc.proc_perspectives(character varying, integer, > character varying, character varying, character varying, integer, date); > > CREATE OR REPLACE FUNCTION dw_bsc.proc_perspectives( > character varying, > integer, > character varying, > character varying, > character varying, > integer, > date) > RETURNS SETOF refcursor AS First you can name your function parameters: http://www.postgresql.org/docs/9.4/interactive/plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS 40.3.1. Declaring Function Parameters If you do that then you can simplify the below. In other words in above the first parameter becomes: v_oper varchar(1) Saves creating a NULL variable and assigning to it as below. > $BODY$ > declare > > v_oper varchar(1) := null; > v_id integer := null; > v_name varchar(50) := null; > v_short_desc varchar(150) := null; > v_descr varchar(500) := null; > v_user_id integer := null; > v_fecha date := null; > v_resu integer := null; > perspectives_cursor refcursor := null; > goals_persps_cursor refcursor := null; > null_cursor refcursor := null; > > > begin > > v_oper := $1; > v_id := $2; > v_name := $3; > v_short_desc := $4; > v_descr := $5; > v_user_id := $6; > v_fecha := $7; > > > -- oper R = READ > -- oper D = DELETE > -- oper M = UPDATE > -- oper I = INSERT > > I can extract the data contained into cursor named perspectives_cursor > in the java application, but the data caontained into cursor named > goals_persps_cursor not. Well you are only returning one refcursor, so that is all you are going to get. To return more than one cursor, modified example from the docs: http://www.postgresql.org/docs/9.4/interactive/plpgsql-cursors.html See bottom of page for original example. CREATE OR REPLACE FUNCTION public.myfunc(cur_a refcursor, cur_b refcursor, arg_1 varchar(1)) RETURNS SETOF refcursor LANGUAGE plpgsql AS $function$ BEGIN RAISE NOTICE 'arg_1 is %', arg_1; OPEN cur_a FOR SELECT * FROM tbl_a; RETURN NEXT $1; OPEN cur_b FOR SELECT * FROM tbl_b; RETURN NEXT $2; END; $function$ ; test=> begin ; BEGIN test=> SELECT * FROM myfunc('a', 'b', '1'); NOTICE: arg_1 is 1 myfunc -------- a b (2 rows) test=> fetch all from a; fld_1 ------- 1 2 3 (3 rows) test=> fetch all from b; fld_1 ------- 4 5 6 (3 rows) See this post from Kevin Grittner for an alternate method: http://www.postgresql.org/message-id/CACjxUsMy_zngFHBia+-QQuR8pOy87VU-L1E6HppWnDU2skjkVw@mail.gmail.com He also includes some notes on how to make this work with JDBC. > > This function was tested by this > > > begin; > select * from dw_bsc.proc_perspectives('R', 1, null, null, null, null, > null); > > fetch all from ""; > end; > > inside the query gui tool provided by pgAdmin III > > The connection into the java application was changed to > con.setAutoCommit(false); > > I think I do not forget nothing else > > Some help will be appreciated very, very, very much !!!!! > > > -----Mensaje original----- > De: Adrian Klaver [mailto:adrian.klaver@aklaver.com] > Enviado el: jueves, 10 de diciembre de 2015 06:25 p.m. > Para: Corradini, Carlos; pgsql-jdbc@postgresql.org; > pgsql-general@postgresql.org > CC: books@ejurka.com > Asunto: Re: [GENERAL] [JDBC] plpgsql function with RETURNS SETOF > refcursor in JAVA > > On 12/10/2015 05:38 AM, Corradini, Carlos wrote: >> Dear Gurus : >> >> First let me say hello from Buenos Aires, Argentina. >> I took this emails addresses from internet ( page www.postgresql.org >> ) >> >> Now I will try to explain which is my problem ( >> excuse my poor level of English, please ). I have a Java application >> that must read a data provided by two ( 2 ) cursors returned by a >> function stored in a database. I know to retrieve data if the function > >> have one ( 1 ) cursor, but with two I can't. I will very pleased if >> any of you, in your free time of course, can explain me how, inside >> the java program, after connecting via jdbc to the database, I extract > >> the data returned by the second cursor. > > Can you provide the function code that is returning the cursors? > >> >> Many thanks ( muchas gracias ) and I wait for yours >> replies as soon as you can. >> > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc