X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5347832C909 for ; Fri, 15 Oct 2004 05:18:51 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 64157-05 for ; Fri, 15 Oct 2004 04:18:48 +0000 (GMT) Received: from fusion.bennyvision.com (fusion.bennyvision.com [216.161.119.28]) by svr1.postgresql.org (Postfix) with SMTP id C591C32C904 for ; Fri, 15 Oct 2004 05:18:47 +0100 (BST) Received: (qmail 28199 invoked from network); 15 Oct 2004 04:18:43 -0000 Received: from localhost (HELO webmail.stinkweasel.net) (127.0.0.1) by fusion.bennyvision.com with SMTP; 15 Oct 2004 04:18:43 -0000 Received: from 63.227.74.41 (SquirrelMail authenticated user benny); by webmail.stinkweasel.net with HTTP; Thu, 14 Oct 2004 23:18:21 -0500 (CDT) Message-ID: <56362.63.227.74.41.1097813923.squirrel@63.227.74.41> Date: Thu, 14 Oct 2004 23:18:21 -0500 (CDT) Subject: Inserting into table only if the row does not already exist. From: "C. Bensend" To: pgsql-sql@postgresql.org User-Agent: SquirrelMail/1.4.3a X-Mailer: SquirrelMail/1.4.3a MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/96 X-Sequence-Number: 19199 Hey folks, I am inserting data into a table with a three-column primary key (the table is only three columns). The rows I'm trying to insert may very well be duplicates of ones already in the table, so I would like to have PostgreSQL handle the insert and possible error resulting from dup data. I can certainly do a SELECT first and then INSERT if it's not a duplicate, ala: * Do a SELECT against the three columns * If there are no rows returned, then do the INSERT But I thought this would be possible with the following: INSERT INTO table ( column1, column2, column3 ) SELECT column1, column2, column3 WHERE NOT EXISTS ( SELECT column1, column2, column3 FROM table WHERE column1 = $column1 AND column2 = $column2 AND column3 = $column3 ) .. which gave me 'ERROR: column1 does not exist'. Nuts. Is this possible to do in one statement? I checked with a DBA friend (he's an Oracle guy), Google, and the list archives, and either didn't find anything helpful or simply didn't try the correct search terms. Or should I be doing this sort of thing in two separate queries? Thanks for all the help you folks have given me, Benny -- "Even if a man chops off your hand with a sword, you still have two nice, sharp bones to stick in his eyes." -- .sig on Slashdot