Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 479519FA111 for ; Mon, 18 Dec 2006 07:28:24 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 43318-08 for ; Mon, 18 Dec 2006 07:28:15 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id AA23C9F931E for ; Mon, 18 Dec 2006 07:28:14 -0400 (AST) Received: (qmail invoked by alias); 18 Dec 2006 11:28:13 -0000 Received: from xdsl-213-196-246-72.netcologne.de (EHLO [192.168.3.10]) [213.196.246.72] by mail.gmx.net (mp041) with SMTP; 18 Dec 2006 12:28:13 +0100 X-Authenticated: #2870507 Message-ID: <45867B49.2090905@gmx.net> Date: Mon, 18 Dec 2006 12:28:09 +0100 From: Roland Walter User-Agent: Thunderbird 1.5.0.8 (X11/20060911) MIME-Version: 1.0 To: Peter Neu CC: pgsql-jdbc@postgresql.org Subject: Re: Efficient Insert statement for one record into multiple References: <01cd01c7228a$6680d960$bcc8a8c0@PCMELZER> In-Reply-To: <01cd01c7228a$6680d960$bcc8a8c0@PCMELZER> X-Enigmail-Version: 0.94.1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/82 X-Sequence-Number: 16314 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Peter Neu wrote: > Hello, > > I have this task that I need to insert one user record into to tables A > and B. > > User table A contains primary key and main data. Table B contains extra > data for > > statistics. Table A has one field Id which is a sequence so it gets a > unique value > > for each new insert. I would like to insert this value into the other > table B as well as > > foreign key. > > 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. > > > > Sorry, for his dumb question but I’m not very experienced in Postgresql. :o( > 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. Regards, Roland. - -- Dipl.-Phys. Roland Walter mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFFhntJxoOEAkary1ERAljXAJ9DASPP58I74Cj6Nt59rDzaIGlHjQCdFZRr lFQGZaGY24/Sj77M6Le9xbc= =Yr/X -----END PGP SIGNATURE-----