BULK insert to import a CSV or TXT file into SQL Server

Source: Internet
Author: User
Tags bulk insert

CSV stands for comma-separated values, sometimes referred to as comma-separated values. and

If the TXT file is loaded, then the file should have a comma-separated value. And the file should be like this

Here is the script to create the table:

CREATE TABLE Employee (

Id int,

Name VARCHAR (100),

Designation VARCHAR (100)

)

I created the location "f a txt and CSV file: \ \ mypublis \ \ 's TXT file name is TestToInsert.txt

Now run the following script to load all the data from the TXT file into the database table. If any errors in any row are not inserted, the other rows are inserted.

I create an ID column in the emloyee table integer, if any row in my file has a string of data, the first part of it refers to going to the ID column so that the row is not inserted and will be continew with the next line.

BULK

INSERT Employee

From ' f:\\mypublis\\testtoinsert.txt '--location with filename

With

(

FieldTerminator = ', ',

Rowterminator = ' \ n '

)

GO

Now see the Data sheet –:

Execute the same script for the CSV file.

With CSV file –:

Siv_csv, CEO

One by one, Brijendra_csv, Operatore

Micro, Company

BULK

INSERT Employee

From ' f:\\mypublis\\csvtest.csv '--location with filename

With

(

FieldTerminator = ', ',

Rowterminator = ' \ n '

)

GO

SELECT *from Employee

The output will be –:

BULK insert to import a CSV or TXT file into SQL Server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.