NO MySQL answers please!
The basic query is as follows (assume A is Key)
INSERT INTO destination (A,B,C)
SELECT a1,b1,c1
FROM source
WHERE (selectconditions) ;
Source contains many records that may or may not already be in destination, which means that the insert will fail as soon as a duplicate record is encountered.
Desired Behaviour: INSERT or IGNORE
This is the desired scenario for the given problem. Insert if you can, otherwise continue.
Pseudo c#/java:
foreach(record in selectQuery)
{
try { destination.insert(record) }
catch(insertionException){//squelch}
}
This can be handled in SQL by adding
AND NOT EXISTS (SELECT A FROM destination INNER JOIN source on destination.A = source.a1)
to the end of the query — In other words, check before you insert.
What are some other alternatives to handling this common situation? What are the pros and cons of these techniques?
Some database provide an explicit syntax for operations that involve a conditional insert/update/ignore.
Oracle and SQLServer, for example have the MERGE statement which can insert/update/delete/ignore a record based on a set of predicates.
Ignoring database-specific syntax, you can perform the insert using a predicate that excludes records that already exist: