SQL server bulk insert for multiple csv files from a single folder. I want store each file as each table at a time I want insert 10 csv and txt files on SQL server. Please provide me any SQL script.

nn 20 Reputation points
2024-07-25T10:02:43.56+00:00

SQL server bulk insert for multiple csv files from a single folder. I want store each file as each table at a time I want insert 10 csv and txt files on SQL server. Please provide me any SQL script.

SQL Server Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2024-07-25T11:18:00.48+00:00
    BULK INSERT tbl1 FROM 'C:\temp\file1.csv' WITH (FORMAT='CSV')
    BULK INSERT tbl2 FROM 'C:\temp\file2.csv' WITH (FORMAT='CSV')
    ...
    BULK INSERT tbl10 FROM 'C:\temp\file10.csv' WITH (FORMAT='CSV')
    

    That is, you need to create the tables before hand. And if you know tell us that you don't know the structure of the files beforehand and can't do that, I am afraid that the answer is that you cannot do this in SQL Server alone, but you need to employ an external tools like SSIS, SQL Server Integration Services.

    0 comments No comments

  2. LiHongMSFT-4306 31,566 Reputation points
    2024-07-26T01:48:23.69+00:00

    Hi @nn

    Have you validated all the answers on your previous post: SQL query to import multiple csv/txt files into SQL Server?

    As I answered, you might need a dynamic query if you do not want to create table or bulk insert files one by one.

    I want store each file as each table at a time I want insert 10 csv and txt files on SQL server. Please provide me any SQL script.

    Have you made any attempts on this issue? Or you have any problems writing the script, if so, please describe the script you wrote and the specific issue.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.