Re: oid's and primary keys on insert - Mailing list pgsql-general

From Nigel J. Andrews
Subject Re: oid's and primary keys on insert
Date
Msg-id Pine.LNX.4.21.0208092315290.3235-100000@ponder.fairway2k.co.uk
Whole thread Raw
In response to Re: oid's and primary keys on insert  (Andrew Sullivan <andrew@libertyrms.info>)
List 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


pgsql-general by date:

Previous
From: "Nigel J. Andrews"
Date:
Subject: Re: uncommited question
Next
From: Peter Eisentraut
Date:
Subject: Re: SQL Comments