Re: oid's and primary keys on insert

From: Andrew Sullivan <andrew(at)libertyrms(dot)info>
To: PostgreSQL General List <pgsql-general(at)postgresql(dot)org>
Subject: Re: oid's and primary keys on insert
Date: 2002-08-09 22:07:31
Message-ID: 20020809180731.R7239@mail.libertyrms.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)libertyrms(dot)info> M6K 3E3
+1 416 646 3304 x110

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nigel J. Andrews 2002-08-09 22:13:47 Re: uncommited question
Previous Message Andrew Sullivan 2002-08-09 22:04:54 Re: oid's and primary keys on insert