Thread: Currval function won't work

Currval function won't work

From
"Bluebottle"
Date:

select currval('library.items_itemid_seq') as NextItemID;

ERROR:  currval of sequence "items_itemid_seq" is not yet defined in this session

 

Note that the nextval function works as expected

 

select nextval('library.items_itemid_seq') as NextItemID;

2313

 

Can somebody please tell me what simple mistake I am making?

 

David

Re: Currval function won't work

From
Michael Fuhr
Date:
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

Re: Currval function won't work

From
Date:

--- Bluebottle <luckychap@bluebottle.com> wrote:

> select currval('library.items_itemid_seq') as
> NextItemID;
> ERROR:  currval of sequence "items_itemid_seq" is
> not yet defined in
> this session
>
> Note that the nextval function works as expected
>
> select nextval('library.items_itemid_seq') as
> NextItemID;
> 2313
>
> Can somebody please tell me what simple mistake I am
> making?
>
> David

David, I don't know the answer, but this is how i
would troubleshoot...

what happens when you try...

1. select currval('library.items_itemid_seq');

2. select currval('library.items_itemid_seq');

3. select currval('library.items_itemid_seq') as
NextItemID;

4. select currval('items_itemid_seq') as NextItemID;

do any of these work?  do any break?

good luck.



__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com

Re: Currval function won't work

From
Sean Davis
Date:
On 10/7/05 1:26 PM, "operationsengineer1@yahoo.com"
<operationsengineer1@yahoo.com> wrote:

>
>
> --- Bluebottle <luckychap@bluebottle.com> wrote:
>
>> select currval('library.items_itemid_seq') as
>> NextItemID;
>> ERROR:  currval of sequence "items_itemid_seq" is
>> not yet defined in
>> this session
>>
>> Note that the nextval function works as expected
>>
>> select nextval('library.items_itemid_seq') as
>> NextItemID;
>> 2313
>>
>> Can somebody please tell me what simple mistake I am
>> making?
>>
>> David
>
> David, I don't know the answer, but this is how i
> would troubleshoot...
>
> what happens when you try...
>
> 1. select currval('library.items_itemid_seq');
>
> 2. select currval('library.items_itemid_seq');
>
> 3. select currval('library.items_itemid_seq') as
> NextItemID;
>
> 4. select currval('items_itemid_seq') as NextItemID;
>
> do any of these work?  do any break?

From the documentation:

http://www.postgresql.org/docs/8.0/interactive/functions-sequence.html

currval

    Return the value most recently obtained by nextval for this sequence in
the current session. (An error is reported if nextval has never been called
for this sequence in this session.) Notice that because this is returning a
session-local value, it gives a predictable answer whether or not other
sessions have executed nextval since the current session did.

Sean