Some tables so you can figure this out.
Table Source
ID Int (Identity)
ContentType Int
Description VarChar(63)
Table Duplicates
ID Int (Identity)
SourceID Int
ContentType Int
Description VarChar(63)
DateReceived DateTime
Table DataType1 (and 2 and 3)
ID Int (Identity)
Description VarChar(63)
Ok
Duplicates
Putting them in Duplicates, NB the structure I'm using will give you a duplicate redord every time the duplicate appears in source,
Insert Into Duplicates(SourceID,ContentType,Description,DateReceived)
Select (ID,ContentType, Description,GetDate())
From
(
Select Source.* From Source,DataType1
Where Source.ContentType = 1
and Source.Description = DataType1.Description
Union
Select Source.* From Source,DataType2
Where Source.ContentType = 2
and Source.Description = DataType2.Description
Select Source.* From Source,DataType1
Where Source.ContentType = 3
and Source.Description = DataType3.Description
) dummyname
And do all the dupiclates
Followed by
Insert Into DataType1(Description)
Select Source.Description
From Source
LEFT OUTER JOIN DataType1
On Source.Description = DataType1.Description
Where Source.ContentType = 1
and DataType1.Description is null
Repeat for tables 2 and 3
The above is about the 'best' version I can come up with in straight sql.
Aside from the union and the sneaky select from (some query) dummyname
NB dummyname can be any valid table name, preferably (not a real one !) The parser insists on it.
The idea is you use a normal join to detect duplicates
And the outer with the where ... is null clause to select all those that aren't in the target table.
It's much better way than doing this
Select * from Table 1 Where Table1.Key not In(select Table2.key From Table2)
NB I used Description as the unique ID to make a point, an integer of some type would of course be more efficient.
If there is going to be fair bit of data building up you'll want to build unique indexes or keys on source and target, then the joins will go well fast.
PS don't forget to housekeep duplicates !
Another nice addition if you are using a stored proc would be to send an email to someone that the job had run (say if it was on the scheduler and may be a a wee snippet of how many duplicates were added.
HtHs
PPS, I didn't run this lot, so spilling mestakes are a probability :p