Skip site navigation (1) Skip section navigation (2)

Re: Last ID Problem

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Mitch Pirtle <mitch(dot)pirtle(at)gmail(dot)com>
Cc: "operationsengineer1(at)yahoo(dot)com" <operationsengineer1(at)yahoo(dot)com>,pgsql-novice(at)postgresql(dot)org
Subject: Re: Last ID Problem
Date: 2005-02-01 01:45:55
Message-ID: 20050201014555.GA52533@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-novice
On Mon, Jan 31, 2005 at 07:58:42PM -0500, Mitch Pirtle wrote:

> That is because you are doing it out of order.  First, you get the
> sequence id, and THEN you use that number for your INSERT statement:

Common practice when using a sequence in PostgreSQL is to do the
INSERT first, then call currval() to find out what value you got.
If you want to obtain the sequence value first then use nextval(),
not currval() as your code showed.  Calling currval() before any
calls to nextval() should fail with an error like the following:

currval of sequence "customer_cust_id_seq" is not yet defined in this session

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

pgsql-novice by date

Next:From: Mike G.Date: 2005-02-01 01:53:55
Subject: Re: pgAdminIII and User Grants
Previous:From: Michael FuhrDate: 2005-02-01 01:38:55
Subject: Re: Last ID Problem

pgsql-hackers by date

Next:From: Mitch PirtleDate: 2005-02-01 01:55:22
Subject: Re: Last ID Problem
Previous:From: Michael FuhrDate: 2005-02-01 01:38:55
Subject: Re: Last ID Problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group