Re: get inserted id from transaction - PG 9.2

From: Steve Atkins <steve(at)blighty(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: get inserted id from transaction - PG 9.2
Date: 2017-02-14 23:16:06
Message-ID: 73520B9C-0AEB-46B9-8898-2B064FDB9D99@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> On Feb 14, 2017, at 2:55 PM, Patrick B <patrickbakerbr(at)gmail(dot)com> wrote:
>
> Hi all,
>
> I'm simply doing an insert and I want to get the inserted id with a select. I'm doing this all in the same transactions.
>
> Example:
>
> BEGIN;
>
> INSERT INTO test (id,name,description) VALUES (default,'test 1','testing insert');
> SELECT FROM test ORDER BY id DESC; -- I don't see the inserted row here

You want "select * from test ..." or "select id from test ..." here. Should work fine then.

>
> COMMIT;
>
> I only can see that inserted row if I do the select outside of this transaction.
>
> How could I get that ?

This'd be the idiomatic way of doing it:

INSERT INTO test (name,description) VALUES ('test 1','testing insert') RETURNING id;

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2017-02-14 23:19:00 Re: get inserted id from transaction - PG 9.2
Previous Message David G. Johnston 2017-02-14 23:11:46 Re: get inserted id from transaction - PG 9.2