I am looking for help to import a .csv
file into SQL Server using BULK INSERT
and I have few basic questions.
Issues:
All of the answers here work great if your data is "clean" (no data constraint violations, etc.) and you have access to putting the file on the server. Some of the answers provided here stop at the first error (PK violation, data-loss error, etc.) and give you one error at a time if using SSMS's built in Import Task. If you want to gather all errors at once (in case you want to tell the person that gave you the .csv file to clean up their data), I recommend the following as an answer. This answer also gives you complete flexibility as you are "writing" the SQL yourself.
Note: I'm going to assume you are running a Windows OS and have access to Excel and SSMS. If not, I'm sure you can tweak this answer to fit your needs.
Using Excel, open your .csv file. In an empty column you will write a formula that will build individual INSERT
statements like =CONCATENATE("INSERT INTO dbo.MyTable (FirstName, LastName) VALUES ('", A1, "', '", B1,"')", CHAR(10), "GO")
where A1 is a cell that has the first name data and A2 has the last name data for example.
CHAR(10)
adds a newline character to the final result and GO
will allow us to run this INSERT
and continue to the next even if there are any errors.Highlight the cell with your =CONCATENATION()
formula
Shift + End to highlight the same column in the rest of your rows
In the ribbon > Home > Editing > Fill > Click Down
Ctrl + C to copy the formulated SQL INSERT
statements
Paste into SSMS
You will notice Excel, probably unexpectedly, added double quotes around each of your INSERT
and GO
commands. This is a "feature" (?) of copying multi-line values out of Excel. You can simply find and replace "INSERT
and GO"
with INSERT
and GO
respectively to clean that up.
Finally you are ready to run your import process
After the process completes, check the Messages window for any errors. You can select all the content (Ctrl + A) and copy into Excel and use a column filter to remove any successful messages and you are left with any and all the errors.
This process will definitely take longer than other answers here, but if your data is "dirty" and full of SQL violations, you can at least gather all the errors at one time and send them to the person that gave you the data, if that is your scenario.