SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server

This is a very common request recently – How to import CSV file into SQL Server? How to load CSV file into SQL Server Database Table? How to load comma delimited file into SQL Server? Let us see the solution in quick steps.

CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values.

Create TestTable

USE TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

Create CSV file in drive C: with name sweetest. text with the following content. The location of the file is C:\csvtest.txt

1,James,Smith,19750101

2,Meggie,Smith,19790122

3,Robert,Smith,20071101

4,Alex,Smith,20040202

SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server csv1

Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.

BULK
INSERT
CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
DROP TABLE CSVTest
GO

SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server csv2

Reference : Pinal Dave (https://blog.sqlauthority.com)

CSV, SQL Scripts, SQL Utility
Previous Post
SQL SERVER – Simple Example of WHILE Loop with BREAK and CONTINUE
Next Post
SQL SERVER – Sharpen Your Basic SQL Server Skills – Database backup demystified

Related Posts

839 Comments. Leave new

  • What is the Max size that SQL server can read and insert into database table? Please help me for this because i am inserting up to 4MB file into a table using .NET code, it is not inserting records in the table. Up to 3 MB files are inserting into the table.

    Thanks

    Reply
  • Hi,
    i have to upload data from a fixed width text file to sql server 2000.

    data in file is like rahulankilprogrammer

    we have two create a table of three column fname,lname and job, first 5 letters will go in first column , next 5 letters will go in second and than next 10 letters will go in third column .

    please help me to do this ,as it is not a CSV file.

    this file come to us on daily basis, daily we hav to upload it on sql server.

    pls pls help me out ASAP

    ThanX in advance

    Reply
  • Hello,
    I have a proplem with BULK in stored procedures.
    I want to do a BULK from a file (.CSV) wich name changes every day. So I have an attribute “@FileName” in order to change the name every single day.

    BULK INSERT dbo.TableName
    FROM @FileName
    WITH ( FIELDTERMINATOR=’;’, ROWTERMINATOR=’\\n’)

    But it gives me an error telling that there’s a sintax error after FROM. I have tried with all kind of punctuation (‘,”,`,´,+)

    It would be gratefull if some one could answer to my question.

    Thanks in advance

    Reply
    • declare @sql varchar(1000)
      set @sq=’BULK INSERT dbo.TableName
      FROM ”’+@FileName+”’
      WITH ( FIELDTERMINATOR=’’;”, ROWTERMINATOR=’’\n’’)’
      EXEC(@sql)

      Reply
  • Vijay Ananth P
    April 29, 2008 5:40 pm

    Hi,
    I try that code. But my saved location is D drive.
    But i shows one error. That is Incorrect syntax near ‘ ‘ ‘.
    Give the solution.

    Thanks,
    Vijay Ananth

    Reply
  • hi
    it solved my problem
    can u please tell how can i schedule stored procedure in sql server2005

    thanks
    rajeh

    Reply
  • Hello,

    I’m using bulk insert and everything works fine but I cannot get latin characters to import properly. For example, my flat data file contains Sertãozinho but the imported data displays Sert+úozinho. The “a” character changes to something with plus sign. I tried using nvarchar datatype but it did not help.

    Here is my code:

    DROP TABLE [DumpData]
    GO
    CREATE TABLE [dbo].[DumpData](
    [DataField1] [varchar](255) NULL
    ON [PRIMARY]

    –import data using bulk insert
    DECLARE @bulk_cmd varchar(1000)
    SET @bulk_cmd = ‘BULK INSERT [NalcoSAPDump]
    FROM ”c:\DataImport\import.dat”
    WITH (FIELDTERMINATOR = ”;”,ROWTERMINATOR = ”’+CHAR(10)+”’)’
    EXEC(@bulk_cmd)

    Thanks!!

    Reply
  • Hello,

    thanks for your answer, Vijay Ananth. :D

    Does anybody know something about incremental bulk? It’s just the “normal” bulk insert or there is another thing that I have to add to the bulk sentence?

    I want to do a BULK from a csv file. Firstly I do a Bulk of the whole file.

    But that csv file changes every time, and in order to have those new rows in my DB I would like to know if there is another way to bulk just the new rows. With that incremental bulk or something else.

    At the moment I’m doing it with bulk every second. So I don’t know if somebody that is writting the file can get an error while the bulk is working.

    Any suggestions will be wellcome.

    Thanks

    Reply
  • Manvendra Singh
    May 6, 2008 3:36 pm

    insert Excel file record in a table + SQL SERVER + C#.net

    Reply
  • Thanks, helpful post.

    Reply
  • Panagiotis Lepeniotis
    May 8, 2008 3:36 am

    Pinal one more time you saved my life, althought it is the first time that I submit you a commend! Mate you are the best! I hope to reach your level one day!

    Panagiotis Lepeniotis
    MCDBA,
    MSc Database Professional Student!

    As for the dataMate who asked about the quotations, I will suggest to edit the csv file, at least thats what I did and worked!

    Cheers

    Reply
  • Hi Pinal,

    i want to batch upload from excel to sqlserver where i want to insert into multiple table as dependency is there between tables. How to do it? Is it possible to do in c#?
    Thank you in advance. I would be very thankful if I get answer quickly as I’m badly in need of it?

    Reply
  • Dave B
    Regarding your question:
    Does BULK INSERTing into an existing table that has an index on it take longer than inserting into an existing table without an index?
    The answer is no. We tried it out on a large Table (10 mil rows) into which we are loading even more rows. WITHOUT the index, the application timed out. With the index, it loaded in fine time.

    Reply
  • Website design Melbouren
    May 27, 2008 2:09 pm

    thanks a lot!

    exactly what i was looking for.

    Reply
  • BULK INSERT CSVTest
    FROM ‘c:\CSVTest.txt’
    WITH
    (
    FIELDTERMINATOR = ‘,’,
    ROWTERMINATOR = ‘\n’
    )
    GO

    I got the following error
    Msg 102, Level 15, State 1, Line 2
    Incorrect syntax near ‘‘’.
    Msg 319, Level 15, State 1, Line 3
    Incorrect syntax near the keyword ‘with’. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

    Can you please let me know what needs to be done in this case?

    Reply
  • Can we do conversions while doing Bulk inserts? (like string to decimal conversions, string to date, string to time etc..,)
    Any suggestions is highly appreciated! :)

    Reply
  • I also get the error: Msg 4860, Level 16, State 1, Line 1
    Cannot bulk load. The file “c:csvtest.txt” does not exist.

    How do I grant SQL access to the file?
    I am running on SQL2005 on an instant I created for myself.

    I tried to change the file to have access approved for ‘everyone’, but still this didn’t make any difference. I still get the file doesn’t exist error.

    Thank you,
    Anke

    Reply
  • I was able to resolve.
    Thank you!

    Reply
  • I have the same problem as Kumar. Runs without errors but only every second (alternative) record gets inserted. I have tried different rowterminators, but the problem remains.

    bulk insert Gen_ExcelImports from ‘C:\Software\invoices\April2008\csvfile.txt’
    with ( FIELDTERMINATOR = ‘,’,FIRSTROW=2,ROWTERMINATOR = ‘\n’)

    Any help would be greatly apprciated. Thanks in advance!

    Reply
  • I have imported a flat (csv) into SQL Server 2005 using the wizard. I have field called Product Code which has 3 numbers e.g. 003. I imported it as text (default). When I open the ipmorted table, i have a plus sign next to the Product Codes e.g. +003. Why is it there and how do i get rid of that plus sign? I just want it to show 003.

    It is particularly annoying and I want to concatenate the Product Codes with other codes to create an ID.

    Please Help

    Reply
    • You should have formatted the cell to be of character type
      or use convert function to convert number to varchar

      Reply
  • Hola your example i good

    How conncted remote computer for read file in this remote computer.

    for example
    BULK
    INSERT dbo.tblPsoArchivosCalificacion

    FROM ‘\\10.63.200.28\Paso\LibroDatos.csv’

    WITH
    (
    FIELDTERMINATOR = ‘,’,
    ROWTERMINATOR = ‘\n’
    )

    the question is?

    i need net use userid and password to access remote computer.

    sorry but my english is bad.

    Reply

Leave a Reply