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!
|