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 1gdGqB-0004Hm-9u for pgsql-general@arkaria.postgresql.org; Sat, 29 Dec 2018 15:47:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gdGq9-0002Uu-VR for pgsql-general@arkaria.postgresql.org; Sat, 29 Dec 2018 15:47:53 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gdGq9-0002Um-NZ for pgsql-general@lists.postgresql.org; Sat, 29 Dec 2018 15:47:53 +0000 Received: from [2001:41c9:1:41d::10] (helo=srv01.teladesign.ie) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gdGq3-00031A-CN for pgsql-general@lists.postgresql.org; Sat, 29 Dec 2018 15:47:53 +0000 Received: from [109.77.214.53] (helo=[192.168.1.148]) by srv01.teladesign.ie with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1gdGq1-0000Yh-1Z; Sat, 29 Dec 2018 15:47:45 +0000 To: Glenn Schultz , pgsql-general@lists.postgresql.org References: From: Ray O'Donnell Message-ID: Date: Sat, 29 Dec 2018 15:47:39 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.3 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 109.77.214.53 X-SA-Exim-Mail-From: ray@rodonnell.ie X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on srv01.teladesign.ie X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.2 Subject: Re: initialize and use variable in query X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:24:06 +0000) X-SA-Exim-Scanned: Yes (on srv01.teladesign.ie) X-Host-Lookup-Failed: Reverse DNS lookup failed for 2001:41c9:1:41d::10 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 29/12/2018 15:40, Glenn Schultz wrote: > All, > > I need to initialize a variable and then use it in query. > Ultimately this will part of a recursive CTE but for now I just need to > work this out.  I followed the docs and thought I needed something like > this.  But does not work-maybe I have misunderstood.  Is this possible? Hi there, What does "does not work" mean? What error do you get? > > SET max_parallel_workers_per_gather = 8; > SET random_page_cost = 1; > SET enable_partitionwise_aggregate = on; > Do $$ > Declare startdate date; > BEGIN > startdate := (select max(fctrdt) from fnmloan); > END $$; A couple of things off the top of my head: (i) I think you need "language plpgsql" (or whatever) after the DO block. (ii) That assignment in the DO should probably be: select max(fctrdt) into startdate from fnmloan; I hope this helps. Ray. -- Raymond O'Donnell // Galway // Ireland ray@rodonnell.ie