Import CSV Files into SQL Server

Did you ever had a CSV file and needed a simple way to import it into SQL Server? If so, try the BULK INSERT command. As long as you do not have any NULL values, you will not find a simpler way.

 

Your CSV file

Create your CSV file (a file with values separated by commas) without a header or an empty line at the end. It can contain umlaute, but not NULL values and should use the encoding UCS-2 BE BOM:

 

Create the table

The table must be an exact match for the values in your CSV file, including the order of the columns:

 

The BULK INSERT command

This BULK INSERT command has various options: You can set the character that splits fields, define how lines are separated, select the encoding (via codepage parameter) and define where in the file the import should start:

 

The Result

When the command has finished you will find your data inside your table in SQL Server:

 

Alternatives when some values are NULL

In this case, the simple command for BULK INSERT is not going to work. A more complex yet more flexible approach is to use format files for the import or the bulk copy program bcp.

 

Conclusion

For simple files is the BULK INSERT a helpful command that can load your CSV file into SQL Server. Try it before you use a more elaborate approach like format files or write your own tool to load the data.

2 thoughts on “Import CSV Files into SQL Server”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.