Original link: http://database.51cto.com/art/200701/37924.htm
BCP is a command line tool used in SQL Server to import and export data. It is based on DB-library and can efficiently import and export large batches of data in parallel. BCP can be used to export tables or views of a database, or filter tables or views using the select from statement to export them. When importing and exporting data, you can use the default value or use a format file to import the data in the file to the database or export the data in the database to the file.
The following describes how to use bcp to import and export data.
1. Introduction to main BCP Parameters
BCP has four actions to choose from.
This operation is completed using the in command, followed by the file name to be imported.
This operation is completed using the out command, followed by the file name to be exported.
(3) use SQL statements to export data.
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 the format file.
This operation is completed using the format command, followed by the format file name.
The following describes some common options:
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.
This option must be used with-F format_file to generate an XML format file.
Specifies the row of the exported table or the row of the imported file.
Specifies the end of the exported table or the end of the imported data.
Char is used as the storage type, with no prefix and "\ t" as the field delimiter, and "\ n" as the row delimiter.
Similar to-C, it is used only when UNICODE character sets are used to copy data, and nchar is used as the storage type.
Specifies the delimiter. The default Delimiter is "\ t ".
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.
Specifies the user name used to connect to the SQL server.
Specify the username and password used to connect to SQL Server.
Specify bcp to log on to SQL Server using a trusted connection. -U and-P must be specified if-T is not specified.
The null value is used to insert an empty column, rather than the default value of this column.
2. How to Use bcp to export data
(1) Use bcp to export the entire table or view.
BCP adventureworks. Sales. Currency out c: \ currency1.txt-c-u "sa"-P "password" -- connect with a password
BCP adventureworks. Sales. Currency out c: \ currency1.txt-c-t -- use a trusted connection
The output result after the preceding command is executed is as follows:
105 rows copied.
Network packet size (bytes): 4096
Clock Time (Ms.) Total: 10 Average: (10500.00 rows per sec .)
The following is part of currency1.txt.
AED Emirati dirham 1998-06-01 00:00:00. 000
AFA Afghani 1998-06-01 00:00:00. 000
Zwd Zimbabwe dollar 1998-06-01 00:00:00. 000
When you log on with a password, you need to add double quotation marks to the user name after-U and the password after-P.
Note: BCP can be executed on the console. You can also run BCP by calling a system stored procedure xp_cmdshell of SQL Server. For example, the first command can be rewritten
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. Currency out c: \ currency1.txt-c-u "sa"-P "password "'
After xp_cmdshell is executed, the returned information is output as a table. To facilitate the execution of BCP in SQL, the following commands use xp_cmdshell to execute the BCP command.
(2) filter the table to be exported.
BCP not only accepts table or view names as parameters, but also SQL as parameters. You can use SQL statements to filter the tables to be exported and then export the filtered records.
Exec master.. xp_mongoshell 'bcp "select top 20 * From adventureworks. Sales. Currency" queryout c: \ currency2.txt-c-u "sa"-P "password "'
BCP can also restrict exported rows by simply setting options.
Exec master .. xp_cmdshell 'bcp "select top 20 * From adventureworks. sales. currency "queryout c: \ currency2.txt-f 10-L 13-c-u" sa "-P" password "'
This command uses two parameters-f 10 and-L 13, which indicate that from select top 20 * From adventureworks. sales. in the result obtained by currency, 10th to 13 records are obtained for export.
3. How to Use bcp to export a format file
BCP not only allows you to import and export data based on tables and views, but also limits the import and export data with format files. A format file exists as a plain text file, which is in the general format and XML format. You can manually write a format file or use the BCP command to automatically generate a format file based on tables and views.
Exec master .. xp_mongoshell 'bcp adventureworks. Sales. Currency format NUL-f c: \ currency_format1.fmt-C-t'
The above command generates a format file currency_format1.fmt for the structure of the currency table. The content of this format file is as follows.
1 sqlchar 0 6 "\ t" 1 currencycode SQL _latin1_general_cp1_ci_as
2 sqlchar 0 100 "\ t" 2 Name SQL _latin1_general_cp1_ci_as
3 sqlchar 0 24 "\ r \ n" 3 modifieddate
This format file records the type, length, character, row delimiter, and field name of the table field (three fields in total.
BCP can also generate xml format files using the-x option.
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. Currency format NUL-f c: \ currency_format2.fmt-X-C-t'
The content described in an XML format file is exactly the same as that described in a common format file, but the format is different.
4. How to Use bcp to import data
Bcpcan use the InCommand to re-import the currency1.txtand currency2.txt imported by the role to the database. Because currency has a primary key, we will copy a table with the same structure as currency.
Select top 0 * into adventureworks. Sales. currency1 from adventureworks. Sales. Currency
Import data to the currency1 table
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. currency1 in c: \ currency1.txt-C-t'
You can also use the-F and-l options to select the record rows for data import.
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. currency1 in c: \ currency1.txt-C-f 10-L 13-t'
When importing data, you can import qualified records to the database based on existing format files. For example, the character length of the Third Field in the preceding format file is 24. If the length of the corresponding field in a text file exceeds 24, this record will not be imported into the database, other matching records are imported normally.
Use common format files
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. currency1 in c: \ currency1.txt-f 10-L 13-C-f c: \ currency_format1.fmt-t'
Use an XML Format File
Exec master.. xp_mongoshell 'bcp adventureworks. Sales. currency1 in c: \ currency1.txt-f 10-L 13-C-X-f c: \ currency_format2.fmt-t'
The BCP command is a quick data import and export tool provided by SQL Server. You can use it to import and export data in an efficient manner without starting any graphical management tool. Of course, it can also be executed in the SQL statement through xp_cmdshell. In this way, it can be placed on the client.Program(Such as Delphi and C.