Let’s say I have a temp table of data I want to put into another table, but before doing so, I want to remove any rows in the temp table that already exist in the destination table.
I’m thinking of using a left outer join to identify which rows are duplicates, but then how can I go and delete these rows from my temp table?
Am I taking the right approach, or would it be better to remove duplicates after transferring the data?
Assuming there is an index on col1/col2, this may perform better than trying to weed out duplicates in the process of the insert:
An equivalent to Jack’s approach would be:
I’m going to assume you have a unique index on the #temp table so you don’t need
DISTINCTorGROUP BY. You can also delete duplicates from the #temp table as a separate step first, e.g.Whether you’re doing the right thing or not, no idea. Will require a lot more information and testing to determine which approach is best. You’re going to have to scan 2+ million rows for duplicates no matter which method you end up using.