Re: Subselect query for a multi table insert single query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Norman Khine" <norman(at)khine(dot)net>
Cc: "Pgsql-Novice" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Subselect query for a multi table insert single query
Date: 2002-09-22 22:03:25
Message-ID: 9267.1032732205@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Norman Khine" <norman(at)khine(dot)net> writes:
> 1) INSERT INTO business_name (business_name, business_url)
> values ('<dtml-var business_name>', '<dtml-var business_url>');

> 2) select last_value from business_name_business_name_seq

Instead use

select currval('business_name_business_name_seq');

to get the assigned sequence value without a race condition. See
http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/functions-sequence.html

Actually you don't need to bother with the separate select, unless
your client code needs that ID for other purposes. You could just
write the currval() call in the second INSERT.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Thomas_Fahey 2002-09-23 02:08:58 writing to external file
Previous Message Norman Khine 2002-09-22 21:17:59 Subselect query for a multi table insert single query