Re: [GENERAL] get inserted id from transaction - PG 9.2 - Mailing list pgsql-general

From Steve Atkins
Subject Re: [GENERAL] get inserted id from transaction - PG 9.2
Date
Msg-id 73520B9C-0AEB-46B9-8898-2B064FDB9D99@blighty.com
Whole thread Raw
In response to [GENERAL] get inserted id from transaction - PG 9.2  (Patrick B <patrickbakerbr@gmail.com>)
List pgsql-general
> On Feb 14, 2017, at 2:55 PM, Patrick B <patrickbakerbr@gmail.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



pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: [GENERAL] get inserted id from transaction - PG 9.2
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] get inserted id from transaction - PG 9.2