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 1ikC1T-0004Qq-Ng for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 19:08:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikC1R-0007kE-Sy for pgsql-general@arkaria.postgresql.org; Wed, 25 Dec 2019 19:08:41 +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 1ikC1R-0007k1-Dt for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 19:08:41 +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 1ikC1K-0000K9-CP for pgsql-general@lists.postgresql.org; Wed, 25 Dec 2019 19:08:39 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47jjKs0B7Nz1GT4; Wed, 25 Dec 2019 14:08:32 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47jjKr5g65zfYc; Wed, 25 Dec 2019 14:08:32 -0500 (EST) Date: Wed, 25 Dec 2019 14:08:32 -0500 From: stan To: Adrian Klaver Cc: pgsql-general@lists.postgresql.org Subject: Re: Syntax question about returning value from an insert Message-ID: <20191225190832.GA21915@panix.com> References: <20191225152638.GA19508@panix.com> <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.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 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 item, 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 inserted in= to > > t2, I an to go ahead and insert it. >=20 > I'm with Pavel in not understanding what you want to do. This prevents 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 First of all, thanks to both of you for your fast response =7F. Let me clarify. 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 other tables. Like Table t1 has a column like cost_category_key So if an INSERT to this table gets called with this column as a NULL, I am creating a function that will query for the default category, like SELECT cost_category_key from t2 where type =3D 'Misc' Now suppose that the default category has not yet been inserted in 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 correct default row will exist in T2, but I still need the (automatically assigned) 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 key is in t2. Make more sense? --=20 "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin