Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> How to insert only records that don't already exist?

How to insert only records that don't already exist?

From: John Haskins <76054.334SPAMBEGONE_at_compuserve.com>
Date: 2000/03/18
Message-ID: <cqAA4.905$bh7.198740@dfiatx1-snr1.gtei.net>#1/1

Greetings, all:

Can anyone tell me how to copy records from one table to another, without the command aborting when 1 or more of the records being copied already exists in the destination table?

The scenario: periodically I get updates of certain reference tables, in which perhaps 10% of the records are actually new since the last update.  I'd like to insert just the new records into the appropriate tables. I've used INSERT INTO to accomplish this in the past, and it works great when the incoming update records don't violate any uniqueness constraints with the existing records. But when they do, the INSERT INTO fails completely, keeping not only the old records but also the new records from being copied to the destination table.

I've tried to filter the update records by comparing them with those already in the destination table. While this seems like a good approach in theory, my execution of it has failed. The code is:

insert into table1
  select key code, key name
  from table2
  where (
    select count(*) from table1
    where table1.key code = table2.key code     ) = 0
;

Can anyone point out how to execute this correctly?

Thanks.

--
Received on Sat Mar 18 2000 - 00:00:00 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US