From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | Patrick B <patrickbakerbr(at)gmail(dot)com> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: get inserted id from transaction - PG 9.2 |
Date: | 2017-02-14 23:11:46 |
Message-ID: | CAKFQuwZR_qTEv=povG6PC+yd_U6v+iDTaZd=PtWpJcDzeHmTBw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Tue, Feb 14, 2017 at 3: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
>
>
>
SELECT <what?> FROM test ...
Written correctly it w
orks for me...
CREATE TABLE testserial (id serial PRIMARY KEY);
BEGIN;
INSERT INTO testserial VALUES (DEFAULT);
SELECT * FROM testserial;
I see one row with id = 1 ...
SELECT version();
version
PostgreSQL 9.3.12 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu
4.8.2-19ubuntu1) 4.8.2, 64-bit
Default transaction isolation level.
COMMIT;
>
>
> I only can see that inserted row if I do the select outside of this
> transaction.
>
> How could I get that ?
>
>
The easiest solution is:
INSERT INTO test [...]
RETURNING id;
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Steve Atkins | 2017-02-14 23:16:06 | Re: get inserted id from transaction - PG 9.2 |
Previous Message | Patrick B | 2017-02-14 22:55:05 | get inserted id from transaction - PG 9.2 |