Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idFhD-0005aH-M2 for pgsql-general@arkaria.postgresql.org; Fri, 06 Dec 2019 15:39:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1idFhC-00038h-H2 for pgsql-general@arkaria.postgresql.org; Fri, 06 Dec 2019 15:39:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1idFhA-00038Z-P2 for pgsql-general@lists.postgresql.org; Fri, 06 Dec 2019 15:39:06 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1idFh7-0007UY-Gp for pgsql-general@lists.postgresql.org; Fri, 06 Dec 2019 15:39:03 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 80EB922ACF; Fri, 6 Dec 2019 10:39:00 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 06 Dec 2019 10:39:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=w vvI4A5NuGuJaxEKmgqvu4ilQbnbdsL7O6emGEXGxtM=; b=VSCwQVSDXMX8iUser pHKqbzEHb7TOD3FYewwYinHMJFOg5a0cdEzKT644RyGDLYfC2TaG02HafG/PfHwq RmfJc9D/mYdBcZj5wU4F7WZIGAuDSDzoLuMny+tBXZI57+J01u3llJ7PXWfeVTPT V3meZWJ2Y1XkgDXrha/L3evnd7WieSBG7Y8fH2wT266dWG3uPuMl7uMUB4VYJJaF DUnJwGrBunJhDj/teCC6Mv6LgF5RO7wFVqjYtCiSKrjatrzcKLnxpWotgJcI7Uxw goZbzBqL0szVd0+RNbMWpqRm880AwPabRep532GNGxULBuvWM0QIRgHIgv+IYMyd Ci5lQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=wvvI4A5NuGuJaxEKmgqvu4ilQbnbdsL7O6emGEXGx tM=; b=gdfmrkN/B5M1lKj5ZVie3vhztVwmHihtzM147gJl8gxUh1gF8UHYLs7tR QXvtb9YW0+FYNRpc0XaRFXWb0LlAU2kSoNrxZDiMJN7jZgiCdTbMFc3MIhM5rgIC 7Ite9byT91n8ukSKbqg06uCpqbQOguLV7nPxxE813kh/JqD9DUE/CfbCD910Obih yo1YpYCnI+HcpL04abkEhH4eS2FKKPQxLqcAYhZff/yOugobr9pWZFwVKqjkhJMM x2l5hMMbYyw5e+6UtMASPx4VzF00RnpDorXjHIzUf4oUpjdGYwRY3trI+365VpC9 zVMsFb0Y1NTktww7gDtsZWM5AzZtA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudekfedgjeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjedurddvuddvrddugeelrdekheenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.149.85]) by mail.messagingengine.com (Postfix) with ESMTPA id CA43B80063; Fri, 6 Dec 2019 10:38:59 -0500 (EST) Subject: Re: Insert Table from Execute String Query To: =?UTF-8?Q?=c4=b0lyas_Derse?= , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Fri, 6 Dec 2019 07:38:58 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/6/19 1:00 AM, İlyas Derse wrote: > I need to insert temp table from execute string query. How can I do ? > I'm trying like that but not working. > > CREATE OR REPLACE FUNCTION public.testdyn > ( >         x integer > ) > RETURNS TABLE > ( >         id bigint, >         text character varying(4000) > ) > AS $$ >         DECLARE mysql TEXT; > BEGIN >         create temp table tmp1 > ( > id1 bigint, > text character varying(4000) > ); > mysql = 'select id, text from TEST'; > > RETURN QUERY >       EXECUTE mysql INTO tmp1 ; > END; > $$ LANGUAGE plpgsql; It would help to know what you are trying to do. In the meantime: 1) create table test (id bigint, text varchar); insert into test values (1, 'test1'), (2, 'test2'); 2) Running your function: select testdyn(1); ERROR: query "SELECT mysql INTO tmp1" is not a SELECT CONTEXT: PL/pgSQL function testdyn(integer) line 11 at RETURN QUERY 3) Change to function: CREATE OR REPLACE FUNCTION public.testdyn(x integer) RETURNS TABLE(id bigint, text character varying) LANGUAGE plpgsql AS $function$ DECLARE mysql TEXT; BEGIN create temp table tmp1 ( id1 bigint, text character varying(4000) ); mysql = 'select id, text from TEST'; drop table tmp1; RETURN QUERY EXECUTE mysql; END; $function$ select * from testdyn(1); id | text ----+------- 1 | test1 2 | test2 -- Adrian Klaver adrian.klaver@aklaver.com