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 1ikGYY-0007bG-HF for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 23:59:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikGYU-0004ur-La for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 23:59: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 1ikGYU-0004ui-50 for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 23:59:06 +0000 Received: from mailbackend.panix.com ([166.84.1.89]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ikGYM-0002QB-Ro for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 23:59:04 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47jqmx5G71z1NW3; Wed, 25 Dec 2019 18:58:57 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47jqmx3l8PzfYc; Wed, 25 Dec 2019 18:58:57 -0500 (EST) Date: Wed, 25 Dec 2019 18:58:57 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Syntax question about returning value from an insert Message-ID: <20191225235857.GA10590@panix.com> References: <20191225152638.GA19508@panix.com> <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com> <20191225190832.GA21915@panix.com> <20e34cf2-0afb-24d0-4d5c-e3fa72f7f726@aklaver.com> <20191225203930.GA29046@panix.com> <29d5c039-9127-aa82-fa8f-1643ee883699@aklaver.com> <20191225230955.GB18451@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20191225230955.GB18451@panix.com> 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 06:09:55PM -0500, stan wrote: > On Wed, Dec 25, 2019 at 02:34:43PM -0800, Adrian Klaver wrote: > > On 12/25/19 12:39 PM, stan wrote: > > >=20 > > > On Wed, Dec 25, 2019 at 11:55:51AM -0800, Adrian Klaver wrote: > > > > On 12/25/19 11:08 AM, stan wrote: > > > > >=20 > > > > > On Wed, Dec 25, 2019 at 08:28:45AM -0800, Adrian Klaver wrote: > > > > > > On 12/25/19 7:26 AM, stan wrote: > > > > > > > I am writing a trigger/function to make certain a default ite= m, and its key > > > > > > > exist when an insert is called. EG > > > > > > >=20 > > > > > > > The trigger gets called on insert to T1 If column c1 is NULL = in the NEW > > > > > > > structure, I need to check table t2 to get the key associated= with the > > > > > > > default for this column. However, if the default is not yet i= nserted into > > > > > > > t2, I an to go ahead and insert it. > > > > > >=20 > > > > > > I'm with Pavel in not understanding what you want to do. This p= revents any > > > > > > clear discussion on what to do below. To help: > > > > > >=20 > > > > > > 1) Schema of t1 and t2. > > > > > >=20 > > > > > > 2) Default for what column? > > > > > >=20 > > > > > > 3) What is the key? > > > > > >=20 > > > > >=20 > > > > > First of all, thanks to both of you for your fast response =7F. > > > > >=20 > > > > > Let me clarify. > > > > >=20 > > > > > I have a table that records will be inserted into. Several of the= columns > > > > > in this table must be non NULL, and they are actually keys from o= ther > > > > > tables. Like > > > > >=20 > > > > > Table t1 > > > > > has a column like cost_category_key > > > > >=20 > > > > > So if an INSERT to this table gets called with this column as a N= ULL, I am > > > > > creating a function that will query for the default category, like > > > > >=20 > > > > > SELECT cost_category_key from t2 where type =3D 'Misc' > > > > >=20 > > > > > Now suppose that the default category has not yet been inserted i= n T2. I > > > > > can easily detect this as the SELECT will return a NULL. So what = I want to > > > > > do is go ahead and insert this row. Once this is done, the correc= t default > > > > > row will exist in T2, but I still need the (automatically assigne= d) key for > > > > > this row to place in the NEW. structure for the function that is = called On > > > > > insert to t1, and checks to see if the value supplied for this ke= y is in > > > > > t2. > > > > >=20 > > > > > Make more sense? > > > >=20 > > > > No. It looks like you are trying to do a backwards FK. I would say = your life > > > > would be a lot easier if you used FK's as intended e.g. have t2 be = the > > > > parent table and INSERT the correct type/key combination there firs= t before > > > > you INSERT into t1, as separate operations. As you script it out ab= ove you > > > > have to know what the the type/key is before you INSERT into t1 any= way. > > >=20 > > > No, the key is auto generated using a sequence on the INSERT into t2 > > >=20 > > > Which is where this gets interesting. If I try to select for it and a= NULL > > > is returned, then I KNOW I need to insert it. As a consequence of thi= s, > > > the new key is generated. I need to populate (replace the NULL) that = comes > > > in the NEW. record with the newly generated key. > > >=20 > > > The original article says that I can get the results of an insert, to= use. > >=20 > > You can given the correct conditions. Since you did not include the syn= tax > > error in the original post it is is difficult to say what the problem i= s and > > I'm not going to just throwing out guesses. >=20 > Sorry, I was trying to just get across what I am trying to do without > writing in large unrelated sections. > >=20 > > >=20 > > > That's what I have in mind. > > >=20 > > > Other approaches welcome. > >=20 > > Other approaches have been suggested, e.g. use a traditional FK > > relationship. The big unknown in you present system is what: > >=20 > > SELECT cost_category_key from t2 where type =3D 'Misc' > >=20 > > will return. The implication is that it maybe more then one value(key) = in > > that case, which key would you use? >=20 > 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.=20 >=20 > 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 >=20 > > If not and cost_category_key: type is one to one then why have both? > > Just make one unique(or PK) and the parent for the cost_category_key in= t1. >=20 Slightly simpler example WITH the syntax error. here is the syntax error psql:src/functions.sql:948: ERROR: syntax error at or near "with" LINE 28: with inserted as ( snippet of function: NEW.project_bom_key =3D with inserted as ( insert into project_cost_category (category) values('MISC') returning project_cost_category_key ) --=20 "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin