Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id F06991337BA2 for ; Sun, 3 Oct 2010 21:47:31 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 41351-06 for ; Mon, 4 Oct 2010 00:47:25 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mailout-de.gmx.net [213.165.64.23]) by mail.postgresql.org (Postfix) with SMTP id 6E3241337B3E for ; Sun, 3 Oct 2010 21:47:24 -0300 (ADT) Received: (qmail invoked by alias); 04 Oct 2010 00:47:23 -0000 Received: from mue-88-130-59-173.dsl.tropolys.de (EHLO [192.168.0.113]) [88.130.59.173] by mail.gmx.net (mp061) with SMTP; 04 Oct 2010 02:47:23 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX19NKmBO9oJNMphYazPLSJ6uP73+u9Las8DJ0COLUc 7ZYz5WgEXsK4T0 Message-ID: <4CA9241E.9030709@gmx.net> Date: Mon, 04 Oct 2010 02:47:26 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.9) Gecko/20100915 Thunderbird/3.1.4 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Need magic for inserting in 2 tables References: <4CA9004E.7060106@gmx.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/9 X-Sequence-Number: 34660 Am 04.10.2010 01:46, schrieb Scott Marlowe: > On Sun, Oct 3, 2010 at 4:14 PM, Andreas wrote: >> insert into staff ( company_fk, ..., department_fk ) >> select company_fk, ..., department_fk >> from departments, companies, company_2_project AS c2p >> where company_id = c2p.company_fk >> and c2p.project_fk = 42 >> and department_id in ( 40, 50 ); >> >> step 2 would be to link those new blank staff records to project 42 by >> inserting a record into staff_2_project for every new staff_id. >> >> How can I find the new staff_ids while making sure I don't insert ids from >> other sessions? >> Is there an elegant way in SQL ? > Use returning? > > insert into ..... > yada > returning field1, field2, field3 It seams the inserts can't be chained? :( The inner insert works when I run it separately but when I run the chained inserts I get an syntax error. How can a script use what RETURNING dumps out? I tried a bit but got nowhere. insert into staff_2_project ( staff_fk, project_fk ) insert into staff ( company_fk, ..., department_fk ) [...] returning staff_id, 42 as project_fk; and insert into staff_2_project ( staff_fk, project_fk ) ( insert into staff ( company_fk, ..., department_fk ) [...] returning staff_id, 42 as project_fk ) as s; and insert into staff_2_project ( staff_fk, project_fk ) select staff_id, project_fk from ( insert into staff ( company_fk, ..., department_fk ) [...] returning staff_id, 42 as project_fk ) as s;