Importing data into database is one of the most important tasks. I often receive questions regarding what is the quickest way to insert CSV data or how to import CSV Data into SQL Server Table. Honestly the process is very simple and the script is even simpler. In today’s SQL in Sixty Seconds Video we will learn how quickly we can insert CSV data into SQL Server.
The steps to import CSV are very simple.
- Create Table
- Use Bulk Insert to import the data
- Verify the data
More on Importing CSV Data:
- SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
- SQL SERVER – Import CSV File into Database Table Using SSIS
- SQL SERVER – Create a Comma Delimited List Using SELECT Clause From Table Column
- SQL SERVER – Comma Separated Values (CSV) from Table Column
- SQL SERVER – Comma Separated Values (CSV) from Table Column – Part 2
I encourage you to submit your ideas for SQL in Sixty Seconds. We will try to accommodate as many as we can.
If we like your idea we promise to share with you educational material.
Reference: Pinal Dave (http://blog.sqlauthority.com)












It may be simple here, but how often is it this simple in the real world with large datasets where data is missing, implicit casts fail etc.?
I have arround 16 crores of records in 16 csv files each having 1 crores of records. It is all about TRAI registred Mobile number and some other information.
I am using Mysql and Sql server also.
I am using bulk insert but it is too slow.
How efficiently I can upload and also Update new date for each week.(update twice in a week)
Please advice.
Brad has a point there. Microsoft’s parser is quite error-prone. Asu, I would advice that you write a CSV parser in whatever platform you’re developing. It’s quite easy. For a .Net example, see: http://pastebin.com/vZgpGNEG
How about a quick video on simplest way to convert a user defined function into a script for testing purposes?
[...] was reading an excellent post from my great friend Pinal about loading data from CSV files, SQL SERVER – Importing CSV File Into Database – SQL in Sixty Seconds #018 – Video, to SQL Server and was honored to write another guest post on SQL Authority about the magic of [...]
What would be really useful is a script that created the table and imported the data to that table. This seems quite tedious. For example, why would I go through all of the trouble creating the table, then creating a script identifying the table headers then importing all of the data.
What I would like to see here is a REAL USEFUL import CSV file, taking the first line as the header (Don’t forget to add the record Numbers Incremental) and then adding the data to those headers. Can this be done?
I am getting below error.
The OLE DB provider “BULK” for linked server “(null)” reported an error. The provider did not give any information about the error.
$value) $data[$key] = addslashes($data[$key]);
$con = mysql_connect($server,$DBusername,$DBpassword);
mysql_select_db($database, $con);
//mysql_connect($server, $DBusername, $DBpassword) or die (“$DatabaseError 54″);
echo $query = mysql_query(“INSERT INTO $tablename (
`name`,
`text`
)
VALUES(‘$data[0]‘, ‘$data[1]‘)”);
$result = mysql_query($query);
}
}
?>
thanks! that was very helpful!
I am using:
BULK INSERT mdb_cleanse.dbo.[@repository employee list]
FROM ‘C:\Users\fa01jjm\Documents\SQL Server Management Studio\Projects\Run Queries\Employee_List.csv’
WITH
(
FIELDTERMINATOR = ‘,’,
ROWTERMINATOR = ‘\n’
)
I get an error saying the OS cannot find the path specified
it’s the file on the same server as the db?
Great post. How can I load a file if I don’t have the name just the directory it suppose to be in?