Re: Unable to use INSERT ... RETURNING with column from other table

From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Unable to use INSERT ... RETURNING with column from other table
Date: 2017-10-24 22:49:05
Message-ID: VisenaEmail.5e.f9aed09bf35804e1.15f509189ac@tc7-visena
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

På onsdag 25. oktober 2017 kl. 00:06:59, skrev Peter Geoghegan <pg(at)bowt(dot)ie
<mailto:pg(at)bowt(dot)ie>>:
On Tue, Oct 24, 2017 at 3:04 PM, Andreas Joseph Krogh
<andreas(at)visena(dot)com> wrote:
> insert into foo(id, name) values(1, 'one'), (2, 'two');
>
> insert into foo(id, name) select 3, f.name from foo f where f.id = 1
returning id, f.id;
>
> ERROR:  missing FROM-clause entry for table "f"
> LINE 1: ...lect 3, f.name from foo f where f.id = 1 returning id, f.id;
>
> I'd like to return f.id and the inserted id, is this possible?

It's possible on 9.5+. You need to assign the target table an alias
using AS -- AS in not a noise word for INSERT (the grammar requires
it).

See the INSERT documentation.
 
I'm not sure how an alias for the target_table will help me here as I'm trying
to return a value not being inserted?
f.id is not inserted, only columns matching f.id.
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com>
www.visena.com <https://www.visena.com>
<https://www.visena.com>

 

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Andreas Joseph Krogh 2017-10-25 00:21:19 Re: Unable to use INSERT ... RETURNING with column from other table
Previous Message Peter Geoghegan 2017-10-24 22:06:59 Re: Unable to use INSERT ... RETURNING with column from other table