Re: Get the last inserted id - Mailing list pgsql-sql

From Scott Marlowe
Subject Re: Get the last inserted id
Date
Msg-id dcc563d10804111041r42de8f4ftc64ac13ec709d273@mail.gmail.com
Whole thread Raw
In response to Get the last inserted id  ("Nacef LABIDI" <nacef.l@gmail.com>)
Responses Re: Get the last inserted id  ("Nacef LABIDI" <nacef.l@gmail.com>)
List pgsql-sql
On Fri, Apr 11, 2008 at 11:23 AM, Nacef LABIDI <nacef.l@gmail.com> wrote:
> Hi all,
>
> I was wondering if postgres can return the last ID inserted for a table
> which the primary key is autoincremented. I need to retrieve this ID in my
> code to continue processing on that inserted row.

smarlowe=# create table test (a serial primary key, b text);
NOTICE:  CREATE TABLE will create implicit sequence "test_a_seq" for
serial column "test.a"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"test_pkey" for table "test"
CREATE TABLE
smarlowe=# insert into test (a,b) values (DEFAULT,'test') returning a;a
---1
(1 row)

INSERT 0 1

Note that there are other ways of doing it, but I really like the
returning clause.


pgsql-sql by date:

Previous
From: "Nacef LABIDI"
Date:
Subject: Get the last inserted id
Next
From: "Nacef LABIDI"
Date:
Subject: Re: Get the last inserted id