Inserting records from csv to the table

  • Ihave 5 csv files that I need to insert to the table. They contain 375000 records in total. Is there solution if one of the records are bad put it in seperate table(Errors) and continue putting good records to the table.

    Bad record would be if this record already exists and I have primary key set for this wan't get duplicate records Thank you

  • if exists (select 1 from DestinationTable where PKField(s) = value(s) from CSV)

    insert into ErrorTable

    else

    insert into DestinationTable

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Is there something from SQL Server I can select to get data from csv and put it to the table. I hace 5 csv files

  • Use SSIS to put these Five files into a staging table, and then use the code that WayneS has provided to copy the new data from the staging table into the destination table.

    If you are using SSIS you can use the execute sql task to run the code

  • yulichka (4/24/2009)


    Is there something from SQL Server I can select to get data from csv and put it to the table. I hace 5 csv files

    You can use the OpenRowset function. Search on google for how to set it up for csv files.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 5 posts - 1 through 5 (of 5 total)

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