Re: What am I doing wrong here?

From: Ray O'Donnell <ray(at)rodonnell(dot)ie>
To: stan <stanb(at)panix(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: What am I doing wrong here?
Date: 2019-12-26 13:55:34
Message-ID: d415686c-9496-53cc-a2e8-7c8c7af84090@rodonnell.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

--
Raymond O'Donnell // Galway // Ireland
ray(at)rodonnell(dot)ie

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jayadevan M 2019-12-26 14:04:28 Re: What am I doing wrong here?
Previous Message Lu, Dan 2019-12-26 13:49:06 RE: Question on upgrading postgresql from 10.7 to 11.5