Sqlserver BCP import and export data

Source: Internet
Author: User

BCP UtilityYou can copy data between an SQL server instance and a data file in a specified format. The BCP utility can be used to import a large number of new rows to SQL Server tables or to import table data to data files. Unless used with the queryout option, you do not need to know about Transact-SQL when using this utility. To import data to a table, you must use the format file created for the table, or you must understand the table structure and the data types that are valid for the columns in the table.

All BCP parameters:

BCP can perform four operations:

(1) Import 
This operation is completed using the in command, followed by the file name to be imported.
(2) Export 
This operation is completed using the out command, followed by the file name to be exported.
(3) export using SQL statements 
This operation is completed using the queryout command. It is similar to out, but the data source is not the table or view name, but an SQL statement.
(4) export a format file 
This operation is completed using the format command, followed by the format file name.

Common parameters:

-F format_file
Format_file indicates the format file name. This option depends on the preceding action. If in or out is used, format_file indicates an existing format file. If format is used, it indicates a format file to be generated.

-X
This option must be used with-F format_file to generate an XML format file.

-F first_row
Specifies the row of the exported table or the row of the imported file.

-L last_row
Specifies the end of the exported table or the end of the imported data.

-C
Char is used as the storage type, with no prefix and "\ t" as the field delimiter, and "\ n" as the row delimiter.

-W
Similar to-C, it is used only when UNICODE character sets are used to copy data, and nchar is used as the storage type.

-T field_term
Specifies the delimiter. The default Delimiter is "\ t ".

-R row_term
Specifies the line delimiter. The default value is "\ n ".

-S SERVER_NAME [\ instance_name]
Specifies the instance of the SQL Server server to be connected. If this option is not specified, BCP connects to the default instance of the local SQL Server. To connect to the default instance on a machine, you only need to specify the machine name.

-U login_id
Specifies the user name used to connect to the SQL server.

-P Password
Specify the username and password used to connect to SQL Server.

-T
Specify bcp to log on to SQL Server using a trusted connection. -U and-P must be specified if-T is not specified.

-K
The null value is used to insert an empty column, rather than the default value of this column.

Common BCP operations

--Export data to tset1.txt and specify the user name and password of the local database--Note: The specified database must have a fully qualified name.
ExecMaster .. xp_mongoshell'BCP "select * From testad. DBO. Test" queryout D: \ tset1.txt-c-u "sa"-P "sasasa"'
--Export data to tset2.xls and use-t to trust the connection--Use-f 10-L 13 to specify the start and end rows of the export.
ExecMaster .. xp_mongoshell'BCP "select * From testad. DBO. Test" queryout D: \ test2.xls-C-f 10-L 13-t"'
--Use the in statement to import data to a data tableExecMaster .. xp_mongoshell'BCP te. DBO. Test in D: \ test2.xls-c-t'

Summary

BCPVery efficient. You can use C # And BCP to import and export data very quickly.

Reference: http://msdn.microsoft.com/zh-cn/library/ms162802.aspx

This article is published in: http://xiaogangblog.com/archives/383

From: http://www.cnblogs.com/xiaogangqq123/archive/2011/09/30/2196727.html

Learn to add to favorites

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.