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'
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'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
SELECT *FROM Employee
output will be-:
History
Keep a going.....