Export and import data using BCP

bcp  between utility can specify the format in Microsoft SQL Server instance and user data files bulk copy data. Use  bcp  utility can be a lot of new rows into SQL Server tables or export table data to the data file. Unless the  queryout  used with the option, or use the utility does not need to understand Transact-SQL knowledge. To import data into a table, a file must be created for the table format, or must understand the structure tables and valid data type for the column in the table.

BCP of all parameters:

 

Four operations that can be performed BCP

(1) import 
this use in order to complete the action, followed by the file name you want to import. 
(2) Export 
This action completed using the out command, with the file name to be exported later. 
(3) the use of SQL statements to export 
the action using queryout command completion, which is similar with out, except that the data source is not a table or view name, but SQL statements. 
(4) export format file 
this action using the format command to complete, with the format and file name.

Common operations:

1. BCP trusted connection to the local export table:

And to export with windows authentication landing text.dbo.name table to the D drive of t_001.txt

2. BCP trusted connection to export the query

3. BCP trusted connection is connected to a remote server Export Query

You can also use the following T-SQL command:

EXEC master..xp_cmdshell 'BCP  test.dbo.name out d:\t_002.txt -c -t -T'

EXEC master..xp_cmdshell 'BCP  "select  name from test.dbo.name" queryout d:\t_004.txt -c -t -T'

EXEC master..xp_cmdshell 'BCP tran_test.dbo.uptrans out d:\t_006.txt -c -t -S HOUYAJUN\JHIDCDBS005 -T'

When such order may be error: error: Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1

SQL Server blocked access to the components 'xp_cmdshell' process 'sys.xp_cmdshell' because this component as part of the security configuration for this server is shut down. The system administrator can enable 'xp_cmdshell' by using sp_configure. For more information about 'xp_cmdshell' is enabled, please see the SQL Server Books Online "Surface Area Configuration."

This time we need to be open:

Copy the code
- allows you to configure advanced options  
EXEC master.sys.sp_configure 'show advanced options', 1  
- Reconfigure  
RECONFIGURE  
- Enable xp_cmdshell  
EXEC master.sys.sp_configure 'xp_cmdshell', 1  
- Reconfigure  
RECONFIGURE  
Copy the code

After running out then turn it off, turn off just need one to zero can be.
The above statement may be out of form by entering a user name and password, the following sentence:

EXEC master..xp_cmdshell 'BCP  test.dbo.name out d:\t_008.txt -c -t  -U"sa" -P"ssssaaaa"'

4. BCP的导入 只需要把上面的 OUT 变为 in 就可以了。

EXEC master..xp_cmdshell 'BCP  test.dbo.name in d:\t_002.txt -c -t  -U -T'

这个是对刚才导出的数据进行重新的导入,可以很明显的看到表中的数据已经增加了。

 

常用参数:

-f format_file 
format_file表示格式文件名。这个选项依赖于上述的动作,如果使用的是in或out,format_file表示已经存在的格式文件,如果使用的是format则表示是要生成的格式文件。

-x 
这个选项要和-f format_file配合使用,以便生成xml格式的格式文件。

-F first_row 
指定从被导出表的哪一行导出,或从被导入文件的哪一行导入。

-L last_row 
指定被导出表要导到哪一行结束,或从被导入文件导数据时,导到哪一行结束。

-c 
使用char类型做为存储类型,没有前缀且以"\t"做为字段分割符,以"\n"做为行分割符。

-w 
和-c类似,只是当使用Unicode字符集拷贝数据时使用,且以nchar做为存储类型。

-t field_term 
指定字符分割符,默认是"\t"。

-r row_term 
specified line separator, the default is "\ n".

-S server_name [\ instance_name] 
Specifies the instance of SQL Server server you want to connect, if this option is not specified, BCP to connect the machine SQL Server default instance. If you want to connect to the default instance on a machine, you only need to specify the machine name.

-U login_id 
specify the connection SQL Sever user name.

-P password 
specifies the user name password to connect to SQL Server.

-T 
Specifies the BCP login using a trusted connection SQL Server. If -T is not specified, you must specify -U and -P.

-k 
specify an empty column with null values into, rather than the default value for this column.

https://www.cnblogs.com/zerocc/p/3225723.html

Guess you like

Origin www.cnblogs.com/shikyoh/p/11089099.html