Forum Discussion
gnanny
Mar 30, 2023Copper Contributor
Bulk Insert causing duplicates in target table
We use ETL tool(C# based) which uses Bulkcopy method to do bulk insert to target table. Recently we are facing 2 Issues in our daily loads 1. Target table with no primary key - the load returns ...
Ronen_Ariely
Apr 02, 2023MVP
Hi gnanny
You probably familiar with the sentence "the total is more than the sum of its parts".
You present two behaviors which we should think about together and not solve each separately.
The combination of "no primary key => loads same records 5 times" and the fact that "when primary key => failure with primary key constraint" clearly is a result of attempting to INSERT the data multiple times. The issue is with your application code as it seems.
using Bulk Insert in the correct way does not INSERT the same row more than once - not if it has PK or not.
Seems like your app run the insert 5 times so if the there is no PK then it INERT the 5 tims and if there is PK then it cannot insert the second time.
To help more than this we will need to see the entire code of the application that relevant to this. In anyway, this means that the issue is not in the SQL Server side and should be in a different forum under .Net or C#.