Click here to Skip to main content
11,641,087 members (64,519 online)
Click here to Skip to main content

Import CSV or txt File Into SQL Server Using Bulk Insert.

, 21 May 2014 CPOL 25.1K 14
Rate this:
Please Sign up or sign in to vote.
Import CSV or txt File Into SQL Server Using Bulk Insert.

Introduction

I am showing here How to import CSV or txt file into SQL Server Database Table. How to load comma delimited file into SQL Server.

Using the code

CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. and
if loading txt file then file should have Comma Delimited Values. and file should be like

Here is the script to create table-:

CREATE TABLE Employee(
Id int,
Name VARCHAR(100),
Designation VARCHAR(100)
)

I Created a txt and a CSV file in location 'F:\\MyPublis\\ with the txt file name is TestToInsert.txt
Now run following script to load all the data from txt file to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
I created Id column as integer in the Emloyee table, if there is any row in my file having first part of the data as a string means that will go to Id column then it will not insert that row and will continew with next row.

BULK
INSERT Employee
FROM 'F:\\MyPublis\\TestToInsert.txt' --location with filename
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

Now see data in table -:

SELECT *FROM Employee

execute same script for the CSV file.

CSV file having the data -:

10, Siv_CSV, CEO
11, Brijendra_CSV, Operatore
12, Micro, Company

BULK 
INSERT Employee
FROM 'F:\\MyPublis\\CSVTest.csv' --location with filename
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
SELECT *FROM Employee

output will be-:

History

Keep a going.....

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralI Vote 5 for this Article Pin
DivyaNaidu48616-Oct-14 20:08
memberDivyaNaidu48616-Oct-14 20:08 
GeneralRe: I Vote 5 for this Article Pin
Arvind Singh Baghel26-Oct-14 23:51
memberArvind Singh Baghel26-Oct-14 23:51 
QuestionMessage Automatically Removed Pin
23-May-14 23:55
membertu vu anh23-May-14 23:55 
QuestionForum for tip/trick: Import CSV or txt File Into SQL Server Using Bulk Insert. Pin
deenasudhakar23-May-14 1:41
groupdeenasudhakar23-May-14 1:41 
AnswerRe: Forum for tip/trick: Import CSV or txt File Into SQL Server Using Bulk Insert. Pin
Arvind Singh Baghel23-May-14 2:31
memberArvind Singh Baghel23-May-14 2:31 
GeneralRe: Forum for tip/trick: Import CSV or txt File Into SQL Server Using Bulk Insert. Pin
deenasudhakar23-May-14 3:08
groupdeenasudhakar23-May-14 3:08 
GeneralRe: Forum for tip/trick: Import CSV or txt File Into SQL Server Using Bulk Insert. Pin
Gary Henning27-May-14 5:08
memberGary Henning27-May-14 5:08 
QuestionVery Nice Pin
dbrenth22-May-14 8:16
memberdbrenth22-May-14 8:16 
Questionnice article Pin
d474221-May-14 23:38
memberd474221-May-14 23:38 
AnswerRe: nice article Pin
Arvind Singh Baghel26-Oct-14 23:52
memberArvind Singh Baghel26-Oct-14 23:52 
GeneralNice article Pin
HardikPatel8921-May-14 22:10
professionalHardikPatel8921-May-14 22:10 
GeneralRe: Nice article Pin
Arvind Singh Baghel26-Oct-14 23:52
memberArvind Singh Baghel26-Oct-14 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 22 May 2014
Article Copyright 2014 by Arvind Singh Baghel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid