Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 6AD471337B8B for ; Sun, 3 Oct 2010 19:14:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 05740-02 for ; Sun, 3 Oct 2010 22:14:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mailout-de.gmx.net [213.165.64.22]) by mail.postgresql.org (Postfix) with SMTP id 769111337B88 for ; Sun, 3 Oct 2010 19:14:38 -0300 (ADT) Received: (qmail invoked by alias); 03 Oct 2010 22:14:36 -0000 Received: from mue-88-130-59-173.dsl.tropolys.de (EHLO [192.168.0.113]) [88.130.59.173] by mail.gmx.net (mp008) with SMTP; 04 Oct 2010 00:14:36 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX1/Vy00Nmy3VGzpMGXIvNAU6EuPOPzSyE8XpWGOdWc BT5JobykeP1M8o Message-ID: <4CA9004E.7060106@gmx.net> Date: Mon, 04 Oct 2010 00:14:38 +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: Need magic for inserting in 2 tables Content-Type: text/plain; charset=UTF-8; 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/7 X-Sequence-Number: 34658 Hi, I need to insert a lot of basically blank records into a table to be filled later. Sounds silly but please bear with me. :) projects ( project_id, project_name, ... ) companies ( company_id, ... ) departments ( department_id, department ) staff ( staff_id SERIAL, company_fk, department_fk, ... ) company_2_project ( project_fk, company_fk ) staff_2_project ( project_fk, staff_fk, project data, ... ) So with this I can store that company 99 belongs e.g. to project 3, 5 and 42 and staff_id 11, 13, 17 belongs to company 99. staff_2_project represents the connection of staff members to a project and holds projectrelated infos. Now say I have allready 100 companies out of the bigger adress pool connected to project 42 and I now want to add blank staffers out of department 40 and 50 linked with this project. I do step 1: 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 ?