Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuKjE-0002xw-H1 for pgsql-general@arkaria.postgresql.org; Tue, 29 Dec 2020 19:32:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kuKjD-0003eK-BP for pgsql-general@arkaria.postgresql.org; Tue, 29 Dec 2020 19:32:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuKjB-0003e8-Jn for pgsql-general@lists.postgresql.org; Tue, 29 Dec 2020 19:32:19 +0000 Received: from [64.147.123.20] (helo=wout4-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuKj7-0001nc-QG for pgsql-general@postgresql.org; Tue, 29 Dec 2020 19:32:16 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 3B00296B; Tue, 29 Dec 2020 14:32:02 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Tue, 29 Dec 2020 14:32:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=y O7rBg8DUiUL5VpJIpsnJKoK43K873uJxcGPw5xaums=; b=ZanAkRZrOxAsmKlma sX5x6I4jgDMnpQJo5iyaK4JtMCr+fRTNjOzxpfSTjjoL+zkJig9pFjRwec5IApiy wiEoRl+sDqPoKiihMyvfUD21QoQAo1oxrIXMffuRXZ3gNxFisHgOPX8wrHYm0MJv zU+4yW9GZ/KFlCrVUnkp+qLP/78wOwMqtOmG29rxh3hSRA908qMlk6K4SFhAel8I 8FLM7+5C11DrIf3jlAXnLhOfhkEaSL6PVfoVzt6JJAlGXKMiIBt6BghoM4UPH/fh MhCHta81MDQLjAWffz5zr/vXE8cuZ7c3VGOqtNC3nbmfx0ltmVJXHsEeEMxB/UFj tqAaw== 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-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=yO7rBg8DUiUL5VpJIpsnJKoK43K873uJxcGPw5xau ms=; b=TVKw06bPsedAeN5yl0map0+SO2FFS0TGnv0wZ24suKUY98hAysDQKo1MA hjqNmZsh+bh5DG/pDcCURcWRIzTPq0zL/2z46VFBOC+QupZZpd+qmkbdcuagf6Z9 INSq1QdCBobBtUOESRRl9JQ5dA5+ko7j6BQ/hFfHha4myGAaXWa9sVD/oUZyKUNI 3+7iv7F6UtzibWtjTtqBESJj6YT0FlCWuxgi5ZXj4NHjCWwYZUT8HD5+2OY3be9p DBnyqEvQcJ5yC8/oTnnISCGM+ftQ76dQjHr7Mbnrzh6Aum5yfjCcFE2BqkF3RNVz XJOLUuOB0HC9VIylRu/coju7X2bAQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvddvuddguddviecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeduudefje etveffhefgudehtdfhvdetkeeiueehfeevveduteekleeuheegudehhfenucffohhmrghi nhepphhoshhtghhrvghsqhhlrdhorhhgnecukfhppeejhedrudejvddrudegrddujeenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgr nhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-14-17.tukw.qwest.net [75.172.14.17]) by mail.messagingengine.com (Postfix) with ESMTPA id EF508240057; Tue, 29 Dec 2020 14:32:00 -0500 (EST) Subject: Re: Dynamic procedure execution To: Mark Johnson Cc: "Muthukumar.GK" , "pgsql-general@postgresql.org >> PG-General Mailing List" References: <5991c08b-6136-b6a9-4979-50d1b355ecdb@aklaver.com> <2ed607fc-12de-4775-185e-b44c14db2fb8@aklaver.com> From: Adrian Klaver Message-ID: <5d2a6b05-5108-0af4-80da-4eb503a2b828@aklaver.com> Date: Tue, 29 Dec 2020 11:32:00 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.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 64.147.123.20 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/29/20 9:29 AM, Mark Johnson wrote: > Don't you have to select into a variable and then return the variable to > the client per [1]? Except PROCEDUREs do not return things(INOUT excepted), it would need to be a FUNCTION. > > Consider the following example from my Oracle system: > > beginning code ... >   V_SQL := 'SELECT COUNT(*) FROM ' || V_TAB; >   EXECUTE IMMEDIATE V_SQL INTO V_CNT; > ending code ... > > [1] > https://www.postgresql.org/docs/current/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN > . > > > > On Tue, Dec 29, 2020 at 11:40 AM Adrian Klaver > > wrote: > > On 12/28/20 10:34 PM, Muthukumar.GK wrote: > > Pleas do not top post, the style on this list is bottom/inline posting. > > Hi Adrian Klaver, > > > > Sorry for typo mistake. Instead of writing lengthy query, I had > written > > it simple. Actually my main concept is to bring result set with > multiple > > rows (using select query) with help of dynamic query. > > > > When calling that procedure in Pgadmin4 window, simply getting the > > message as ‘ CALL     Query returned successfully in 158 msec’. > > > > FYI, I have implemented simple dynamic query for UPDATE and > DELETE rows. > > It is working fine without any issues. > > > > Please let me know is there any way of getting result set using > dynamic > > query. > > > > _Issue with dynamic select:-_ > > > > __ > > > > CREATE OR REPLACE Procedure sp_select_dynamic_sql( > > > >                  keyvalue integer) > > > >      LANGUAGE 'plpgsql' > > > > AS $BODY$ > > > > Declare v_query text; > > > > BEGIN > > > >                  v_query:= 'select * from Los_BankInfo ' > > > >          || ' where pk_id = ' > > > >          || quote_literal(keyvalue); > > > >                  execute v_query; > > > >                  END; > > > > $BODY$; > > > > _Execuion__ of Proc:-_ > > > > CALL sp_select_dynamic_sql (11); > > > > _Output:-_ > > > > CALL > > > > Query returned successfully in 158 msec. > > See here: > > https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING-PROCEDURE > > > " > 42.6.2. Returning from a Procedure > > A procedure does not have a return value. A procedure can therefore end > without a RETURN statement. If you wish to use a RETURN statement to > exit the code early, write just RETURN with no expression. > > If the procedure has output parameters, the final values of the output > parameter variables will be returned to the caller. > " > > So use a function and follow the docs here: > > https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNING > > > in particular: > > "42.6.1.2. RETURN NEXT and RETURN QUERY" > > > > > _Working fine with Dynamic UPDATE and DELETE Statement :-_ > > > > __ > > > > _UPDATE:-_ > > > > __ > > > > CREATE OR REPLACE Procedure sp_Update_dynamic_sql( > > > >                  newvalue varchar(10), > > > >                  keyvalue integer) > > > >      LANGUAGE 'plpgsql' > > > > AS $BODY$ > > > > Declare v_query text; > > > > BEGIN > > > >                  v_query:= 'update Los_BankInfo set approverid' > > > >          || ' = ' > > > >          || quote_literal(newvalue) > > > >          || ' where pk_id = ' > > > >          || quote_literal(keyvalue); > > > >          execute v_query; > > > >                  END; > > > > $BODY$; > > > > --CALL sp_Update_dynamic_sql (john,10); > > > > _DELETE:-_ > > > > __ > > > > CREATE OR REPLACE Procedure sp_Delete_dynamic_sql( > > > >                  keyvalue integer) > > > >      LANGUAGE 'plpgsql' > > > > AS $BODY$ > > > > Declare v_query text; > > > > BEGIN > > > >                  v_query:= 'delete from Los_BankInfo ' > > > >          || ' where pk_id = ' > > > >          || quote_literal(keyvalue); > > > >                  execute v_query; > > > >                  END; > > > > $BODY$; > > > > --CALL sp_Delete_dynamic_sql(10); > > > > > > > > Regards > > > > Muthu > > > > > > On Mon, Dec 14, 2020, 8:54 PM Adrian Klaver > > > >> wrote: > > > >     On 12/13/20 9:59 PM, Muthukumar.GK wrote: > >      > Hi team, > >      > > >      > When I am trying to implement belwo dynamic concept in > postgreSql, > >      > getting some error. Kindly find the below attached program and > >     error. > >      > Please advise me what is wrong here.. > >      > > >      > CREATE OR REPLACE PROCEDURE DynamicProc() > >      > > >      > AS $$ > >      > > >      > DECLARE v_query TEXT; > >      > > >      > C1 refcursor := 'result1'; > >      > > >      > begin > >      > > >      > v_query := ''; > >      > > >      > v_query := ' OPEN C1 FOR SELECT * FROM public."Bankdetails"'; > >      > > >      > EXECUTE (v_query); > >      > > >      > END; > >      > > >      > $$ > >      > > >      > Language plpgsql; > >      > > >      >   Calling procedure :- > >      > > >      > -------------------------------- > >      > > >      > CALL DynamicProc(); > >      > > >      > FETCH ALL IN "result1"; > >      > > >      > > >      > Error :- > >      > > >      > -------------- > >      > > >      > ERROR: syntax error at or near "OPEN" > >      > > >      > LINE 1: OPEN C1 FOR SELECT * FROM public."Los_BankInfo" ^ > >      > > >      > QUERY: OPEN C1 FOR SELECT * FROM public."Los_BankInfo" > >      > > >      > CONTEXT: PL/pgSQL function dynamicproc() line 9 at EXECUTE SQL > >     state: 42601 > > > >     Two things: > > > >     1) The error is from a different version of the procedure > then the > >     code. > >     The table name is different. Can't be sure that this is the only > >     change. > >     So can you synchronize your code with the error. > > > >     2) Take a look here: > > > > https://www.postgresql.org/docs/12/plpgsql-cursors.html > > >      > > > > >     42.7.2. Opening Cursors > > > >     For why OPEN is plpgsql specific and how to use it. > > > >      > > >      > > >      > Regards > >      > > >      > Muthukumar.gk > >      > > > > > > >     -- > >     Adrian Klaver > > adrian.klaver@aklaver.com > > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > -- Adrian Klaver adrian.klaver@aklaver.com