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

From Andrew Sullivan
Subject Re: oid's and primary keys on insert
Date
Msg-id 20020809180731.R7239@mail.libertyrms.com
Whole thread Raw
In response to Re: oid's and primary keys on insert  ("Nigel J. Andrews" <nandrews@investsystems.co.uk>)
Responses Re: oid's and primary keys on insert
List pgsql-general
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').  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.)

A

--
----
Andrew Sullivan                               87 Mowat Avenue
Liberty RMS                           Toronto, Ontario Canada
<andrew@libertyrms.info>                              M6K 3E3
                                         +1 416 646 3304 x110


pgsql-general by date:

Previous
From: Andrew Sullivan
Date:
Subject: Re: oid's and primary keys on insert
Next
From: "Nigel J. Andrews"
Date:
Subject: Re: uncommited question