Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid re-inserting data (duplicates) into SQL Server table while re-running SSIS package that loads data?

Tags:

I have created a package is SSIS. It's working fine for first time insertion. When I am running the package through SQL Server agent jobs, I am getting duplicates inserted when the scheduled job is inserting data.

I don't have any idea about how to stop inserting multiple duplicate records.

I am expecting to remove duplicates insertion while running deployed package through SQL Server Jobs

enter image description here

like image 721
Raj Esh Avatar asked Mar 31 '19 05:03

Raj Esh


People also ask

How do I stop inserting duplicate records in SSIS?

You can use a Lookup transformation to get the non-matching rows between Source and destination and ignore duplicates: UNDERSTAND SSIS LOOKUP TRANSFORMATION WITH AN EXAMPLE STEP BY STEP. SSIS - only insert rows that do not exists. SSIS import data or insert data if no match.

How can we prevent insertion of duplicate data in SQL?

Preventing Duplicates from Occurring in a Table. You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.

How do you exclude duplicate records?

Click Data > Remove Duplicates, and then Under Columns, check or uncheck the columns where you want to remove the duplicates.


1 Answers

There are 2 approaches to do that:

(1) using SQL Command

This option can be used if source and destination are on the same server

Since you are using ADO.NET source you can change the Data Access mode to SQL Command and select only data that not exists in the destination:

SELECT *
FROM SourceTable
WHERE NOT EXISTS(
                SELECT 1
                FROM DestinationTable
                WHERE SourceTable.ID = DestinationColumn.ID)

(2) using Lookup Transformation

You can use a Lookup transformation to get the non-matching rows between Source and destination and ignore duplicates:

  • UNDERSTAND SSIS LOOKUP TRANSFORMATION WITH AN EXAMPLE STEP BY STEP
  • SSIS - only insert rows that do not exists
  • SSIS import data or insert data if no match
  • Implementing Lookup Logic in SQL Server Integration Services
like image 120
Hadi Avatar answered Oct 19 '22 16:10

Hadi