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

Re: PLSQL Question regarding multiple inserts

From: Richard Huxton <dev(at)archonet(dot)com>
To: "Humble Geek" <humblegeek(at)rogers(dot)com>,pgsql-general(at)postgresql(dot)org
Subject: Re: PLSQL Question regarding multiple inserts
Date: 2004-02-29 15:54:12
Message-ID: 200402291554.12814.dev@archonet.com (view raw or flat)
Thread:
Lists: pgsql-general
On Wednesday 25 February 2004 04:11, Humble Geek wrote:
> Hi all. Quick and perhaps silly question, but...
>
> I am using Pg 7.3. I am writing a function using pgplsql. This function
> will perform multiple inserts. Let's say two of the inserts are as follows:
>
> -- id is primary key
> insert into users (id, username) values (nextval('someSeq'),'somename');
> insert into log (id, uid, message) values (nextval('someOtherSeq'),XXX,'New
> Account');
>
> Assume XXX is the id from the first insert. How do I get that number? Not
> currval('someSeq') -  'cause someone else may have performed an insert -
> but the id for that specific insert.

Sequences are safe to use in multi-user environments. That is, currval() will 
return the most recent value nextval() returned *in this connection*. 
Wouldn't be much use otherwise.

The easiest way to demonstrate this is to open two psql sessions and try it 
for yourself.

-- 
  Richard Huxton
  Archonet Ltd

In response to

pgsql-general by date

Next:From: Aleksey SerbaDate: 2004-02-29 16:12:05
Subject: inlined sql functions in 7.4
Previous:From: Geoffrey KinnelDate: 2004-02-29 15:06:13
Subject: Re: Where are the backend/commands?

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