Re: how to return the last inserted identity column value

From: Ondrej Ivanič <ondrej(dot)ivanic(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: how to return the last inserted identity column value
Date: 2012-03-09 00:01:47
Message-ID: CAM6mie+8_g1KAunU1oDfgWCDy-RrO3E8a4Ceo1+OvK6vUGGH+A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

On 9 March 2012 05:20, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com> wrote:
> 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;

You can use lastval() or currval() functions:
http://www.postgresql.org/docs/9.1/static/functions-sequence.html

--
Ondrej Ivanic
(ondrej(dot)ivanic(at)gmail(dot)com)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Frank Church 2012-03-09 00:09:27 Re: How to erase transaction logs on PostgreSQL
Previous Message Ondrej Ivanič 2012-03-08 22:59:22 Re: Synchronous replication + Fusion-io = waste of money OR significant performance boost? (compared to normal SATA-based SSD-disks)?