Re: Subselect query for a multi table insert single query - Mailing list pgsql-novice

From Tom Lane
Subject Re: Subselect query for a multi table insert single query
Date
Msg-id 9267.1032732205@sss.pgh.pa.us
Whole thread Raw
In response to Subselect query for a multi table insert single query  ("Norman Khine" <norman@khine.net>)
List pgsql-novice
"Norman Khine" <norman@khine.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

pgsql-novice by date:

Previous
From: "Norman Khine"
Date:
Subject: Subselect query for a multi table insert single query
Next
From: "Thomas_Fahey"
Date:
Subject: writing to external file