Re: Select nextval problem

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: SZUCS Gábor <surrano(at)mailbox(dot)hu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Select nextval problem
Date: 2002-11-27 23:41:52
Message-ID: 20021127234152.GA29884@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Nov 27, 2002 at 06:15:04PM +0100, SZUCS Gábor wrote:
> 2. There is something called currval(...) that doesn't increase the counter,
> just returns its current value. In our lucky case, you need something that
> can be queried with this function, so I'd say either of the following will
> do:
>
> SELECT * FROM product WHERE prodid = currval('prodid_seq');
> SELECT * FROM product ORDER BY prodid DESC LIMIT 1;
>
> Both of these, however, assume that you haven't inserted any rows after the
> one in question.

Wrong. The second one does. The first guarenteed to return what the earlier
nextval() returned. It is therefore the recommended method. Lookup the
documentation for more details.

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Support bacteria! They're the only culture some people have.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nigel J. Andrews 2002-11-28 00:20:00 Why does everyone think MySQL is easier?
Previous Message Tom Lane 2002-11-27 23:29:12 Re: Shared library