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 1ikRyt-0001C0-DI for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 12:11:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikRyr-0006m7-Fj for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 12:11:05 +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 1ikRyr-0006m0-5m for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 12:11:05 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikRyj-0004vL-P6 for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 12:11:04 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47k81W5CYNz1dnd; Thu, 26 Dec 2019 07:10:55 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47k81W3cFRzfYc; Thu, 26 Dec 2019 07:10:55 -0500 (EST) Date: Thu, 26 Dec 2019 07:10:55 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Syntax question about returning value from an insert Message-ID: <20191226121055.GA20722@panix.com> References: <20191225230955.GB18451@panix.com> <0BC9EA4D-B1F0-4354-BE2C-80BA8E12097B@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Editor: gVim User-Agent: Mutt/1.12.1 (2019-06-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Dec 25, 2019 at 09:17:22PM -0800, Adrian Klaver wrote: > On 12/25/19 4:48 PM, Rob Sargent wrote: > > > > > > > On Dec 25, 2019, at 3:10 PM, stan wrote: > > > There is more that that. There is a project number, so the actuall key > > > represents the combination of project number, and cost category, Thire is a > > > constraint on T2 that assures that these combinations will be unique. > > > > > > When the 1st record for a project, that gets charged to the misc. cost > > > category is entered, I need to assure that combination gets inserted into > > > T2 > > > > Is mine the only nose reacting to this? Cost categories aren???t > > specific to projects are they? Do you need more than a definition of > > cost categories and then project specific > > No, my nose is twitching also. > > expenditures use the category id? Here is this mornings update on this task. I am learning about CTE's, which is something that was on my need to do list anyway. So I have created this test script, which works: WITH inserted AS ( INSERT into project_bom (project_key, bom_name) VALUES (1 , 'test') RETURNING project_bom_key ) SELECT project_bom_key FROM inserted BUT, when I try to assign the result of SELECT to either the NEW. record or a declared variable, such as _bom_name_key , I get syntax error, for example. ERROR: syntax error at or near "_bom_name_key" LINE 35: _bom_name_key = ( SELECT project_bom_key Is this because the whole CTE segment is a separate "query". How can I preserve the results of the SELECT to use outside the CTE? I really appreciate the help you folks are providing. -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin