-2

i have table that have one column phoneNO primary key i want to upload csv file via sqlbulkcopy i have error that primary key not insert duplicate value so my question is how to ignore row if it's duplicate and other rows inserts in database

leppie
  • 115,091
  • 17
  • 196
  • 297

1 Answers1

1

This is a multiple step process which addresses duplicates in the csv files and records which already exist within the database.

  1. Create temporary table table w/out phoneNumber primary key and add extra fields if necessary
  2. Insert all csv files into temporary table via sqlbulkcopy
  3. Once all csv files have been inserted into temporary table, perform select distinct with a where conditional making sure all newly inserted values do not already exist in the destination table.

Creating a temporary table

CREATE TABLE TemporaryTable
([phoneNumber] varchar(13));

Perform Select/Distinct with a where clause excluding all records already found within

Insert YourTableName (phoneNumber)
Select distinct phoneNumber
FROM TemporaryTable
WHERE phoneNumber not in (select phoneNumber
                          from YourTableName)
WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
  • Hi @user3157452 if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – WorkSmarter Apr 13 '15 at 14:37