Skip to content
Advertisement

SQL – How to avoid inserting duplicate rows from another table while matching multiple columns

Using Microsoft SSMS.

There are two tables

Table1

TempTable

I want to insert only the last row in TempTable to Table1, because the other 2 records are present already.

A row is a duplicate only when all the column values in TempTable match with a record on Table1.

I’m not sure how to use NOT IN or INTERSECT or any other alternate, as there are multiple column values to be matched.

UPDATE:

Following @llyes suggestion, helped me resolve this.

Advertisement

Answer

You could use EXCEPT as the following

Demo

You could also use NOT EXISTS() as

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement