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 1ikUbZ-0007Si-Bt for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 14:59:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ikUbX-00012A-ED for pgsql-general@arkaria.postgresql.org; Thu, 26 Dec 2019 14:59:11 +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 1ikUbX-00011r-16 for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 14:59:11 +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 1ikUbU-0000ia-3G for pgsql-general@lists.postgresql.org; Thu, 26 Dec 2019 14:59:09 +0000 Received: from panix5.panix.com (panix5.panix.com [166.84.1.5]) by mailbackend.panix.com (Postfix) with ESMTP id 47kClZ6YHcznff; Thu, 26 Dec 2019 09:59:06 -0500 (EST) Received: by panix5.panix.com (Postfix, from userid 17622) id 47kClZ58T6zfYc; Thu, 26 Dec 2019 09:59:06 -0500 (EST) Date: Thu, 26 Dec 2019 09:59:06 -0500 From: stan To: Ray O'Donnell Cc: pgsql-general@lists.postgresql.org Subject: Re: What am I doing wrong here? Message-ID: <20191226145906.GA21962@panix.com> References: <20191226133639.GA15506@panix.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 Thu, Dec 26, 2019 at 01:55:34PM +0000, Ray O'Donnell wrote: > On 26/12/2019 13:36, stan wrote: > > IF _bom_name_key is NULL > > THEN > > WITH inserted AS ( > > INSERT into project_bom > > (project_key, bom_name) > > VALUES > > (NEW.project_key , 'Main') > > RETURNING > > project_bom_key > > ) > > /* Syntax error flagged at this line */ > > _bom_name_key = ( SELECT > > project_bom_key > > FROM inserted ) > > ; > > ELSE > > NEW.project_bom_key = _bom_name_key; > > END IF; > > END IF; > > > You need to use the SELECT INTO syntax: > > with inserted as ( > .... > ) > select project_bom_key into _bom_name_key > from inserted > (etc) > > Likewise, while I don't think there's anything wrong with the earlier > assignment - > > _bom_name_key := (select....); > > - to my eye the SELECT INTO looks more natural: > > select project_bom_key > into _bom_name_key > from ... (etc). > > It's a PL/pgSQL construct - full details here: > > https://www.postgresql.org/docs/12/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW > > I hope that this helps. > > Ray. Thanks for the quick response. Yes, that solved my issue. RE different syntax. Yes I need to be more careful to be consistent in the way I do things. I have a tendency to do the same thing different ways when there are multiple ways of doing the same thing. -- "They that would give up essential liberty for temporary safety deserve neither liberty nor safety." -- Benjamin Franklin