Ateeq
Ateeq

Reputation: 823

duplicate rows using insert into

I have a table that contains data and I want to use insert into statement here is my query

  INSERT INTO [table]
  SELECT Distinct * FROM [DataSource]

how to to ignore a row from being inserted if it's already found in the table?

Upvotes: 0

Views: 101

Answers (1)

Kevin Suchlicki
Kevin Suchlicki

Reputation: 3145

Assuming your target table and your data source table have identical structure, you could do something like this:

INSERT INTO [table]
SELECT Distinct * 
FROM [DataSource]
EXCEPT
SELECT * 
FROM [table]

Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.

Upvotes: 1

Related Questions