Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E70FB9F9398 for ; Mon, 18 Dec 2006 10:51:14 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 18064-01-6 for ; Mon, 18 Dec 2006 10:50:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 871389FB20D for ; Mon, 18 Dec 2006 10:49:52 -0400 (AST) Received: (qmail 26327 invoked from network); 18 Dec 2006 14:49:50 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 18 Dec 2006 14:49:50 -0000 In-Reply-To: <52EF20B2E3209443BC37736D00C3C1380BF08073@EXADV1.host.magwien.gv.at> References: <52EF20B2E3209443BC37736D00C3C1380BF08073@EXADV1.host.magwien.gv.at> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <3D7E9990-2CC9-4129-8732-69DDAA6817CD@fastcrypt.com> Cc: "Roland Walter *EXTERN*" , "Peter Neu" , Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Efficient Insert statement for one record into multiple Date: Mon, 18 Dec 2006 09:50:13 -0500 To: Albe Laurenz X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/87 X-Sequence-Number: 16319 On 18-Dec-06, at 9:36 AM, Albe Laurenz wrote: >>> I have this task that I need to insert one user record into to >>> tables > A >>> and B. >>> >>> So I'd like to do something like this >>> >>> insert into A (user_name, value2 ) values ( "foo", "foo2") >>> >>> AND >>> >>> insert into B ( id_from_a , statistic_data) values ( 23, "bla") >>> >>> How is this done the best way? I would like to do this in one call. >> >> If you know the name of the sequence for the primary key in table A, > say >> "primary_A", then use the following to insert into table B direct > after >> you inserted into table A in the same connection: >> >> insert into B (id_from_a, statistic_data) values( > currval("primary_A"), >> bla") >> >> This works only if your insertion into table A called in some form >> nextval("primary_A"), may it be as default value or in the >> insertion-statement itself. > > This is _bad_. No, this works > > What if somebody did a nextval("primary_A") between the insert into A > and the insert into B? as long as you use currval then it works as advertised > > If there is a sequence "primary_A" for table A, you should proceed > as follows: This requires two trips to the db. Roland's solution doesn't, and it does work. > > - select nextval("primary_A") > - then insert into A with this primary key > - then insert into B with the same key > > That should all be in a single transaction! This doesn't even have to be in a single transaction. Dave > > Yours, > Laurenz Albe > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org >