Monday, May 9, 2011

How to prevent duplicate insertion in sqlbulkcopy operation

i need to import multiple excel file sheet into Sql Server Database. i implement an ASP.Net Application to allow user upload those excel file and read all the data into the database. However i had found an issue that the sqlbulkcopy can quickly copy and import all the data into sql server. However the sqlbulkcopy is a straight copy. there is no option to detect the duplicate record or duplicate import during the process.

though we can enforce the duplciation detection in the sqlbulkcopy action. we still are able to prevent the duplicate records insertion in the table level.. we only create a unique table index for the target table then the index will filter all those duplciate record out during the quick imported by sqlbulkcopy.



here is a sample sinppet of sql code.

CREATE UNIQUE NONCLUSTERED INDEX [MyIndexName] ON [dbo].[MyTable]
(
    [Column1] ASC,
    [Column2] ASC,
    [Column3] ASC,
    [Column4] ASC,
   )
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = ON, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO


the criteria to create a UNIQUE NONCLUSTERED INDEX is to select all the columns that can unqiuely identify the record in the target imported table

2 comments:

  1. Thank you for sharing this information. I find this information is easy to understand and very useful. Thumbs up!


    ReplyDelete
  2. Thank you for sharing this information. I was very intrigued by this solution. Good Work.

    ReplyDelete