SQL Server uses BULK INSERT to import a text to database-http://www.wangchao.net.cn/it/detail_59013.html

Source: Internet
Author: User
Tags bulk insert
This is very common request recently-how to import CSV file into SQL server? How to load CSV file into SQL Server database table? How to load comma delimited file into SQL server? Let us see the solution in quick steps.
CSV stands for comma separated values, sometimes also called comma delimited values.
Create testtable
Use testdata
Go
Create Table csvtest
(ID int,
Firstname varchar (40 ),
Lastname varchar (40 ),
Birthdate smalldatetime)
Go
Create CSV file in drive C: With name csvtest.txt with following content. The location of the file is c: \ csvtest.txt
1, James, Smith, 19750101
2, Meggie, Smith, 19790122
3, Robert, Smith, 20071101
4, Alex, Smith, 20040202
Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
Bulk
Insert csvtest
From 'C: \ csvtest.txt'
With
(
Fieldterminator = ',',
Rowterminator = '\ N'
)
Go
-- Check the content of the table.
Select *
From csvtest
Go
-- Drop the table to clean up database.
Select *
From csvtest
Go

This is very common request recently-how to import CSV file into SQL server? How to load CSV file into SQL Server database table? How to load comma delimited file into SQL server? Let us see the solution in quick steps. CSV stands for comma separated values, sometimes also called comma delimited values. create testtable use testdata go create table csvtest (ID int, firstname varchar (40), lastname varchar (40), birthdate smalldatetime) Go create CSV file in drive C: with name csvtest.txt with following content. the location of the file is c: \ csvtest.txt 1, James, Smith, 19750101 2, Meggie, Smith, 19790122 3, Robert, Smith, 20071101 4, Alex, Smith, 20040202 now run following script to load all the data from CSV to database table. if there is any error in any row it will be not inserted but other rows will be inserted. bulk insert csvtest from 'C: \ csvtest.txt 'with (fieldterminator =', ', rowterminator =' \ n') Go -- check the content of the table. select * From csvtest go -- drop the table to clean up database. select * From csvtest go

Related Article

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.