Import CSV and TXT data into the database

Source: Internet
Author: User
Tags bulk insert

The simplest option for importing text data into a database is that Excel uses Openset,csv and txt with BULK insert to implement

1. Use OPENROWSET to import Excel into the database

Refer to "OpenRowSet usage"

2, use BULK INSERT to import TXT data into the database, text data using tab to split column, use newline character to split row.

--Create staging TableCreate Tabledbo.txt_staging (col1nvarchar(255), col2nvarchar(255), Col3nvarchar(255))Go--Populate DataBulk Insertdbo.txt_staging fromN'D:\test.txt' with(FieldTerminator= '\ t', Rowterminator= '\ n')

3. Use BULK INSERT to import CSV data into the database
The CSV file uses "," as the column delimiter, using "\ n" as the row delimiter

--Populate DataBulk Insertdbo.txt_staging fromN'D:\abc.csv' with(FieldTerminator=N',', Rowterminator=N'\ n', CODEPAGE=N'Raw')

Appendix

The syntax of the BULK INSERT is simplified as follows

 bulk  insert   schema_name. table_name  from    data_file    with   (FieldTerminator  =   '  

FieldTerminator ='field_terminator'

Specifies the field terminator to is used for char and widechar data files. The default field terminator is \ t (tab character).

Rowterminator ='row_terminator'

Specifies the row terminator to is used for char and widechar data files. The default row terminator is \ r \ n (newline character).

Reference doc:

BULK INSERT (Transact-SQL)

Import CSV and TXT data into the database

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.