Re: oid's and primary keys on insert

From: "Nigel J(dot) Andrews" <nandrews(at)investsystems(dot)co(dot)uk>
To: Andrew Sullivan <andrew(at)libertyrms(dot)info>
Cc: PostgreSQL General List <pgsql-general(at)postgresql(dot)org>
Subject: Re: oid's and primary keys on insert
Date: 2002-08-09 22:20:12
Message-ID: Pine.LNX.4.21.0208092315290.3235-100000@ponder.fairway2k.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 9 Aug 2002, Andrew Sullivan wrote:

> On Fri, Aug 09, 2002 at 11:01:30PM +0100, Nigel J. Andrews wrote:
> > accepted method of do this sort of thing with sequences is either:
> >
> > 1. SELECT nextval('seqname');
> > 2. Remember the result in variable say currid
> > 3. INSERT INTO mytable (idcol, ...) VALUES (<value from currid>, ...)
> > 4. Use value from variable currid whereever else it is that you require it
> >
> > or
> >
> > 1. INSERT INTO mytable (idcol, ...) VALUES (<value from currid>, ...)
> > 2. SELECT curval('seqname');
> > 3. Remember the result in variable say currid
> > 4. Use value from variable currid whereever else it is that you require it
>
> In the second example, you don't need to get "currid". You can just
> do INSERT and then SELECT currval('seqname').

Oops, that's wine, cut and paste for you. I just rearranged and renumbered the
lines from the first version.

> Remember, currval is
> defined as sticking _with your connection_, so you don't have to
> worry about someone else messing with your currval. (That's also the
> reason you can't get a currval() until you have SELECTed nextval(),
> either implicitly or explicitly.)

Yes, as Andrew points out in step 1 of version 2 the <value from currid> should
be: nextval('seqname') and that the application variable currid can be avoided
by just selecting curval('seqname') whereever it's needed.

--
Nigel J. Andrews
Director

---
Logictree Systems Limited
Computer Consultants

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2002-08-09 22:55:16 Re: SQL Comments
Previous Message Nigel J. Andrews 2002-08-09 22:13:47 Re: uncommited question