From: Adrian Klaver <adrian.klaver@aklaver.com>
To: stan <stanb@panix.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Syntax question about returning value from an insert
Date: Wed, 25 Dec 2019 08:28:45 -0800
Message-ID: <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com> (raw)
In-Reply-To: <20191225152638.GA19508@panix.com>
References: <20191225152638.GA19508@panix.com>
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
>
> 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 into
> t2, I an to go ahead and insert it.
I'm with Pavel in not understanding what you want to do. This prevents
any clear discussion on what to do below. To help:
1) Schema of t1 and t2.
2) Default for what column?
3) What is the key?
>
> I found this page:
> https://stackoverflow.com/questions/6560447/can-i-use-return-value-of-insert-returning-in-another-in...
> which sugest this syntax:
>
> with rows as (
> INSERT INTO Table1 (name) VALUES ('a_title') RETURNING id
> )
> INSERT INTO Table2 (val)
> SELECT id
> FROM rows
>
> I modified it slightly to look like this:
>
> IF _bom_name_key is NULL
> THEN
> with rows as (
> INSERT INTO project_bom (bom_name) VALUES ('Main') RETURNING
> project_bom_key
> )
> NEW.project_bom_key = SELECT project_bom_key
> FROM rows ;
>
> But this gives me syntax error.
>
> I realize this functionality is slightly different, but can I get the new
> key into the NEW structure to return from the function call?
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, stanb@panix.com
Subject: Re: Syntax question about returning value from an insert
In-Reply-To: <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.com>
References: <20191225152638.GA19508@panix.com> <6b104cf5-554e-89d0-8392-443f5f57c52c@aklaver.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