Re: [GENERAL] get inserted id from transaction - PG 9.2

Поиск
Список
Период
Сортировка
От Steve Atkins
Тема Re: [GENERAL] get inserted id from transaction - PG 9.2
Дата
Msg-id 73520B9C-0AEB-46B9-8898-2B064FDB9D99@blighty.com
обсуждение исходный текст
Ответ на [GENERAL] get inserted id from transaction - PG 9.2  (Patrick B <patrickbakerbr@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: [GENERAL] get inserted id from transaction - PG 9.2
Следующее
От: Tom Lane
Дата:
Сообщение: Re: [GENERAL] get inserted id from transaction - PG 9.2