Enable BCP: exec sp_configure 'show advanced options', 1; reconfigure; Exec sp_configure 'xp _ cmdshell', 1; reconfigure; 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. 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.
1. Introduction to main BCP Parameters
BCP has four actions to choose from.
(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) 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:
-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.
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 or 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:
Starting copy...
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: In addition to running BCP on the console, you can also run BCP using SQL statements 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.
This command uses two parameters-f 10 and-L 13 to indicate
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 "'
Lect top 20 * From adventureworks. Sales. Currency, 10th to 13 records are retrieved 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.
9.0
3
1 sqlchar 0 6 running /t "1 currencycode SQL _latin1_general_cp1_ci_as
2 sqlchar 0 100 usd/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.