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

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 (view raw or flat)
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

pgsql-novice by date

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

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