Re: sequences and currval()

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: "Michael P(dot) Soulier" <michael_soulier(at)mitel(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: sequences and currval()
Date: 2008-06-02 16:02:16
Message-ID: 20080602160216.GD27752@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, Jun 02, 2008 at 11:55:14AM -0400, Michael P. Soulier wrote:
> Hello,
>
> I'm migrating a db schema in an automated fashion, using this
>
> UPDATE clients_client
> SET icp_id = null
> WHERE icp_id = 1;
> UPDATE icps_icp
> SET id = nextval('public.icps_icp_id_seq')
> WHERE id = 1;
> UPDATE clients_client
> SET icp_id = currval('public.icps_icp_id_seq')
> WHERE icp_id = null;

Your problem is that this should be: WHERE icp_id IS NULL.

> I've noticed this on a fresh pgsql session.
>
> tugdb=# select currval('icps_icp_id_seq');
> ERROR: currval of sequence "icps_icp_id_seq" is not yet defined in this
> session
>
> I don't understand this, as I only want the current value of the
> sequence. I suppose I can get it this way

currval() returns the value last returned by nextval() *in this
session*. If you havn't called it in this session ofcourse it won't
work. This is for transaction safety.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-06-02 16:53:31 Re: dblink() cursor error/issue (TopMemoryContext)
Previous Message Michael P. Soulier 2008-06-02 15:55:14 sequences and currval()