pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: stan <stanb@panix.com>
To: Ray O'Donnell <ray@rodonnell.ie>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: What am I doing wrong here?
Date: Thu, 26 Dec 2019 09:59:06 -0500
Message-ID: <20191226145906.GA21962@panix.com> (raw)
In-Reply-To: <d415686c-9496-53cc-a2e8-7c8c7af84090@rodonnell.ie>
References: <20191226133639.GA15506@panix.com>
	<d415686c-9496-53cc-a2e8-7c8c7af84090@rodonnell.ie>


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





view thread (7+ messages)  latest in thread

Message-ID: <20191226145906.GA21962@panix.com>
Permalink:  ../20191226145906.GA21962@panix.com/
Also on:    postgresql.org/message-id/20191226145906.GA21962@panix.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: stanb@panix.com, ray@rodonnell.ie
  Subject: Re: What am I doing wrong here?
  In-Reply-To: <20191226145906.GA21962@panix.com>
  References: <20191226133639.GA15506@panix.com> <d415686c-9496-53cc-a2e8-7c8c7af84090@rodonnell.ie> <20191226145906.GA21962@panix.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox