CaptainDriftwood
CaptainDriftwood

Reputation: 5708

SQL Server copy existing table into new one but skip duplicate records

So I have an empty target table that I am wanting to copy all the records into from an existing Report table. However, the existing Report table does not have any primary keys, and my new target table does. I want to copy over all the records from the existing Report table that are not duplicates on "Report.field1" and "Report.field2" and they are not NULL in either one as well.

Is there a quick and dirt way to do that? Like:

INSERT INTO target REPORT
ON CONFLICT SKIP

Upvotes: 0

Views: 185

Answers (1)

Marta B
Marta B

Reputation: 448

Please see: How to avoid duplicate SQL data while executing the INSERT queries without source database

IF not exists(select * from Report where field1 = @field1 and field2 = @field2) and @field1 is not null and @field2 is not null
    INSERT INTO Report (..., ..., ...) 
        VALUES (..., ..., ...)

Upvotes: 1

Related Questions