Sam Cromer
Sam Cromer

Reputation: 707

Filling live table with records from staging table

I have 2 Identical tables in SQL Server 2008, one is the "Live" table which my application is constantly using. I am pulling down new records periodically during the day and inserting them into a "staging" table from which I need to pull new records and insert into the Live table. I don't want any duplicates inserted just in case some records are overlapped. There are 10 columns which I need to look at to see if an identical record exists, I have looked at some TSQL examples but none so far are working, I have also considered dealing with the dupes and just reporting on the DISTINCT values but DISTINCT one works for one record, I need it to work for 10. Any suggestions?

Thanks, Sam

Upvotes: 5

Views: 283

Answers (1)

JNK
JNK

Reputation: 65157

This is an ideal use case for NOT EXISTS - you can check on as many criteria as you like to verify you won't insert a dupe.

INSERT INTO Live
SELECT <fields>
FROM Staging s
WHERE NOT EXISTS (SELECT 1
                  FROM Live l
                  WHERE s.FieldA = l.FieldA
                  AND s.FieldB = l.FieldB
                  <all your checks here>...)

Upvotes: 5

Related Questions