SQL query to import multiple csv/txt files into SQL Server.

nn 20 Reputation points
2024-07-23T09:23:05.23+00:00

SQL query to import multiple csv/txt files into SQL Server.

SQL Server | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,581 Reputation points
    2024-07-23T09:38:04.49+00:00

    You will need one query per different file sturcture.

    See

    https://learn.microsoft.com/en-us/sql/t-sql/functions/openrowset-transact-sql?view=sql-server-ver16

    for how to.


  2. LiHongMSFT-4306 31,616 Reputation points
    2024-07-24T02:11:28.93+00:00

    Hi @nn

    To import multiple csv/txt files into SQL Server using SQL query, you typically need two steps:

    First, you need to list all files inside a folder to a table.

    Then generate dynamic bulk insert query to import these files.

    It is suggested to use SSIS to accomplish this, see this blog: SSIS - How To Load Multiple Files ( .txt or .csv ) To a Table With File Names.

    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.