Re: getting back autonumber just inserted

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Theodore Petrosky <tedpet5(at)yahoo(dot)com>
Cc: mail TechEvolution <mail(at)techevolution(dot)be>, pgsql-sql(at)postgresql(dot)org
Subject: Re: getting back autonumber just inserted
Date: 2005-07-07 20:21:49
Message-ID: 1120767709.8208.188.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 2005-07-07 at 15:14, Theodore Petrosky wrote:
> you have to use currval inside a transaction...
>
> begin;
> insert something that increments the counter;
> select currval('sequence_name');
> end;
>
> using currval inside a transaction guarantees that the
> value is correct for your insert statement and has not
> changed by another insert statement.

Actually, whether you use it inside a transaction or not, as long as
it's in the same session / connection, it is guaranteed to "do the right
thing."

Test it, you'll see.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message mail TechEvolution 2005-07-07 20:24:54 Re: getting back autonumber just inserted
Previous Message Theodore Petrosky 2005-07-07 20:14:33 Re: getting back autonumber just inserted