Import from CSV append to table but ignore duplicates

  • How can I import data from a CSV into an existing table but ignore duplicate records?

  • You usually create a staging table where you import the data.  Then you insert the "valid" records with something like this :

     

    Insert into dbo.RealTable (Key, Col1, Col2) Select Key, Col1, Col2 from dbo.Staging S where not exists (Select Key from dbo.RealTable RT where S.Key = RT.Key)

     

     

    That will only insert data that is not already in the table based on a single column primary key.

  • Works like a charm. Thank you. Thank you. Wow there is so much to learn about SQL.

  • 6000 + posts and now I have to start all over again with sql 2005 .  Hopefully I have a headstart this time .

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply