Re: Currval function won't work - Mailing list pgsql-novice

From Michael Fuhr
Subject Re: Currval function won't work
Date
Msg-id 20051007075639.GA59072@winnie.fuhr.org
Whole thread Raw
In response to Currval function won't work  ("Bluebottle" <luckychap@bluebottle.com>)
List pgsql-novice
On Fri, Oct 07, 2005 at 04:54:13PM -0700, Bluebottle wrote:
> select currval('library.items_itemid_seq') as NextItemID;
> ERROR:  currval of sequence "items_itemid_seq" is not yet defined in
> this session

currval() returns the value most recently obtained from nextval()
in the current session.  If you haven't called nextval() yet then
you get the error above.  Example:

test=> CREATE SEQUENCE foo_seq;
CREATE SEQUENCE
test=> SELECT currval('foo_seq');
ERROR:  currval of sequence "foo_seq" is not yet defined in this session
test=> SELECT nextval('foo_seq');
 nextval
---------
       1
(1 row)

test=> SELECT currval('foo_seq');
 currval
---------
       1
(1 row)

> Note that the nextval function works as expected
>
> select nextval('library.items_itemid_seq') as NextItemID;
> 2313

You should be able to call currval() after calling nextval().  If
not then please tell us a little more about your environment, such
as whether you're using a connection pool.

--
Michael Fuhr

pgsql-novice by date:

Previous
From: "Bluebottle"
Date:
Subject: Currval function won't work
Next
From: "Tjibbe"
Date:
Subject: TEMPORARY TABLE in a PL/pgSQL function