Re: how to return the last inserted identity column value

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: mgould(at)isstrucksoftware(dot)net
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to return the last inserted identity column value
Date: 2012-03-08 18:20:25
Message-ID: CAOR=d=1qALu8uBkcxi239WADErnxeuvge3w-mgKMWp6t-eaTMw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Mar 8, 2012 at 11:16 AM, <mgould(at)isstrucksoftware(dot)net> wrote:
> In some languges you can use set l_localid = @@identity which returns
> the value of the identity column defined in the table.  How can I do
> this in Postgres 9.1

Assuming you created a table like so:

smarlowe=# create table test (id serial,info text);
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for
serial column "test.id"
CREATE TABLE

Then use returning:

smarlowe=# insert into test (info) values ('this is a test') returning id;
id
----
1
(1 row)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike Blackwell 2012-03-08 18:45:17 Re: [BUGS] Altering a table with a rowtype column
Previous Message mgould 2012-03-08 18:16:24 how to return the last inserted identity column value