Importing CSV File Into SQL Server Using Bulk Insert

Importing CSV File data into SQL Server table is a commonly asked question that has an easy answer. Find out how to use SQL Bulk Insert.

By Tim TrottSQL and mySql Tutorials • March 15, 2010
Importing CSV File Into SQL Server Using Bulk Insert

A CSV file has one row per line, and column values separated with a common, which gives it the name Comma Separated Values. It does not necessarily have to be comma-separated, it could be separated with tabs or some other delimiter. CSV files are almost universally supported and a good way of transporting database records between systems.

To import a CSV into SQL Server you must first create the table. In this example, the table is called Products and has three columns, product SKU, product name and product description.

sql
CREATE TABLE Products
(
  productSku VARCHAR(10),
  productName VARCHAR(20),
  productDescription VARCHAR(255),
);
GO

The data I am going to load is stored in the file C:\Tutorials\SQLBulkInsertTest.csv, the contents of which are shown below.

ABC123,Test Product,This is a test product to test the bulk insert function
ABC987,Another Test Product,This is another test product
XYZ444,Yet Another Test,This is yet another test product for SQL Server

Importing CSV File Using Bulk Insert

To import this data into the test Products table, we are going to use the Bulk Insert command.

sql
BULK INSERT Products
FROM 'C:TutorialsSQLBulkInsertTest.csv'
WITH
(
  FIELDTERMINATOR = ',',
  ROWTERMINATOR = 'n'
)
GO

Where FIELDTERMINATOR is the character used to separate column values and ROWTERMINATOR is used to split records. In this case, the defaults are to split values using a comma and a new line marks the end of a record.

By viewing the table data now, we can see that the data has been successfully imported.

sql
SELECT * FROM Products
productSku productName productDescription
ABC123 Test Product This is a test product to test the bulk insert function
ABC987 Another Test Product This is another test product
XYZ444 Yet Another Test This is yet another test product for SQL Server

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.