Loading CSV file to database using BULK INSERT
Below are the step by step approach to easily load data from CSV file into SQL Server database table.
- First create the table in the database corresponding to CSV file
CREATE TABLE NameList
(
FirstName varchar(40),
LastName varchar(40)
)
- Now, generate format file for the above table using
BCP Format
option
C:\Users\venkat>BCP master.dbo.NameList format nul
-x -S ServerName -c -f c:\dev\NameListFormat.xml -t, -T
- Now, load data into the table using
BULK INSERT
TRUNCATE TABLE dbo.NameList;
BULK INSERT dbo.NameList
FROM 'c:\dev\NameList.csv'
WITH (FORMATFILE = 'C:\dev\NameListFormat.xml');
GO
- Now, data is successfully loaded from CSV to SQL Server table.
No comments:
Post a Comment