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
Asked
Active
Viewed 882 times
-2
-
1I'd import it into a temporary table in SQL then do a select distinct/insert into the final table. – Lloyd Apr 13 '15 at 14:00
-
Open in Excel and click "Remove Duplicates" and then perform sqlbulkcopy. – WorkSmarter Apr 13 '15 at 14:01
-
Lloyd i have thousds of csv file and one csv file have millons of number – user3157452 Apr 13 '15 at 14:02
-
WorkSmarter if number alredy inserted in table then ??? – user3157452 Apr 13 '15 at 14:03
-
The proposed solution will handle that scenario also. – WorkSmarter Apr 15 '15 at 02:52
1 Answers
1
This is a multiple step process which addresses duplicates in the csv files and records which already exist within the database.
- Create temporary table table w/out phoneNumber primary key and add extra fields if necessary
- Insert all csv files into temporary table via sqlbulkcopy
- 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