In IT environment, it is always a necessary to import data from data files into database.
And recently, I have to deal with import a huge load of data (around 200 MB for 1 CSV input file) from CSV files into MS SQL SERVER. So what I had done to achieve this is by using BULK INSERT.
Firstly, prepare a CSV data file with the following content and save as test.csv.
And then run scripts as following to load all the data from the CSV file into database.
And recently, I have to deal with import a huge load of data (around 200 MB for 1 CSV input file) from CSV files into MS SQL SERVER. So what I had done to achieve this is by using BULK INSERT.
Firstly, prepare a CSV data file with the following content and save as test.csv.
July,Singapore,25
James,Australia,50
May,China,29And then run scripts as following to load all the data from the CSV file into database.
--Temp table to store the data
CREATE TABLE CSVTest
(
Name VARCHAR(500),
Country VARCHAR(500),
Age INT
)
GO
--Bulk insert into CSVTest
BULK INSERT CSVTest
FROM 'C:\test.csv'
WITH
(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n'
)
GO
--Get all data from CSVTest
SELECT *
FROM CSVTest
GO
--Drop the temp table
DROP TABLE CSVTest
GO