Re: Get the last inserted id

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Nacef LABIDI" <nacef(dot)l(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Get the last inserted id
Date: 2008-04-11 17:41:41
Message-ID: dcc563d10804111041r42de8f4ftc64ac13ec709d273@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Fri, Apr 11, 2008 at 11:23 AM, Nacef LABIDI <nacef(dot)l(at)gmail(dot)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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Nacef LABIDI 2008-04-11 17:43:56 Re: Get the last inserted id
Previous Message Nacef LABIDI 2008-04-11 17:23:04 Get the last inserted id