View Single Post
 
Old September 10th, 2007, 01:58 AM
elygp elygp is offline
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting unique records from a table to another

Hi All,

Can anyone please help me with this? I want to insert records to a table coming from another table. The condition is that, insert should only insert records from the source table with ID not in the target table. My code is something like this:

   INSERT targetTable
  (
      counterparty_id,
      counterparty_name,
      related,
      rba,
      country_code,
      cp_code
   )

     SELECT counterparty,
     counterparty_name,
     counterparty_type_id,
     domiciled_description,
     '0',
     '0'

      FROM

       sourceTable


   WHERE
    counterparty <> '' AND
      not exists (Select counterparty_id from targetTable)


There are records in the source table with the same counterparty. And these records with the same counterparty should be copied only once or if they are not yet in the target table.

Thanks in advance!