bcp Utility
The bcp utility bulk copies data between an instance of Microsoft SQL Server and a data file in a user-specified format. The bcp utility can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files. Except when used with the queryout option, the utility requires no knowledge of Transact-SQL. To import data into a table, you must either use a format file created for that table or understand the structure of the table and the types of data that are valid for its columns.
For the syntax conventions that are used for the bcp syntax, see Transact-SQL Syntax Conventions (Transact-SQL).
Note
|
|---|
|
If you use bcp to back up your data, create a format file to record the data format. bcp data files do not include any schema or format information, so if a table or view is dropped and you do not have a format file, you may be unable to import the data. |
bcp [database_name.] schema.{table_name | view_name | "query" {in data_file | out data_file | queryout data_file | format nul}
[-a packet_size]
[-b batch_size]
[-c]
[-C { ACP | OEM | RAW | code_page } ]
[-d database_name]
[-e err_file]
[-E]
[-f format_file]
[-F first_row]
[-h"hint [,...n]"]
[-i input_file]
[-k]
[-K application_intent]
[-L last_row]
[-m max_errors]
[-n]
[-N]
[-o output_file]
[-P password]
[-q]
[-r row_term]
[-R]
[-S [server_name[\instance_name]]
[-t field_term]
[-T]
[-U login_id]
[-v]
[-V (80 | 90 | 100 )]
[-w]
[-x]
/?
The bcp 11.0 client is installed when you install Microsoft SQL Server 2012 tools. If tools are installed for both SQL Server 2012 and an earlier version of SQL Server, depending on the value of the PATH environment variable, you might be using the earlier bcp client instead of the bcp 11.0 client. This environment variable defines the set of directories used by Windows to search for executable files. To discover which version you are using, run the bcp /v command at the Windows Command Prompt. For information about how to set the command path in the PATH environment variable, see Windows Help.
XML format files are only supported when SQL Server tools are installed together with SQL Server Native Client.
For information about where to find or how to run the bcp utility and about the command prompt utilities syntax conventions, see Command Prompt Utility Reference (Database Engine).
For information on preparing data for bulk import or export operations, see Prepare Data for Bulk Export or Import (SQL Server).
For information about when row-insert operations that are performed by bulk import are logged in the transaction log, see Prerequisites for Minimal Logging in Bulk Import.
Native Data File Support
In SQL Server 2012, the bcp utility supports native data files compatible with SQL Server 2000, SQL Server 2005, SQL Server 2008, and SQL Server 2008 R2 only.
Computed Columns and timestamp Columns
Values in the data file being imported for computed or timestamp columns are ignored, and SQL Server automatically assigns values. If the data file does not contain values for the computed or timestamp columns in the table, use a format file to specify that the computed or timestamp columns in the table should be skipped when importing data; SQL Server automatically assigns values for the column.
Computed and timestamp columns are bulk copied from SQL Server to a data file as usual.
Specifying Identifiers That Contain Spaces or Quotation Marks
SQL Server identifiers can include characters such as embedded spaces and quotation marks. Such identifiers must be treated as follows:
-
When you specify an identifier or file name that includes a space or quotation mark at the command prompt, enclose the identifier in quotation marks ("").
For example, the following bcp out command creates a data file named Currency Types.dat:
bcp AdventureWorks2012.Sales.Currency out "Currency Types.dat" -T -c
-
To specify a database name that contains a space or quotation mark, you must use the -q option.
-
For owner, table, or view names that contain embedded spaces or quotation marks, you can either:
-
Specify the -q option, or
-
Enclose the owner, table, or view name in brackets ([]) inside the quotation marks.
-
Data Validation
bcp now enforces data validation and data checks that might cause scripts to fail if they are executed on invalid data in a data file. For example, bcp now verifies that:
-
The native representation of float or real data types are valid.
-
Unicode data has an even-byte length.
Forms of invalid data that could be bulk imported in earlier versions of SQL Server might fail to load now; whereas, in earlier versions, the failure did not occur until a client tried to access the invalid data. The added validation minimizes surprises when querying the data after bulk load.
Bulk Exporting or Importing SQLXML Documents
To bulk export or import SQLXML data, use one of the following data types in your format file.
|
Data type |
Effect |
|---|---|
|
SQLCHAR or SQLVARYCHAR |
The data is sent in the client code page or in the code page implied by the collation). The effect is the same as specifying the -c switch without specifying a format file. |
|
SQLNCHAR or SQLNVARCHAR |
The data is sent as Unicode. The effect is the same as specifying the -w switch without specifying a format file. |
|
SQLBINARY or SQLVARYBIN |
The data is sent without any conversion. |
A bcp out operation requires SELECT permission on the source table.
A bcp in operation minimally requires SELECT/INSERT permissions on the target table. In addition, ALTER TABLE permission is required if any of the following is true:
-
Constraints exist and the CHECK_CONSTRAINTS hint is not specified.
Note
Disabling constraints is the default behavior. To enable constraints explicitly, use the -h option with the CHECK_CONSTRAINTS hint.
-
Triggers exist and the FIRE_TRIGGER hint is not specified.
Note
By default, triggers are not fired. To fire triggers explicitly, use the -h option with the FIRE_TRIGGERS hint.
-
You use the -E option to import identity values from a data file.
Note
|
|---|
|
Requiring ALTER TABLE permission on the target table was new in SQL Server 2005. This new requirement might cause bcp scripts that do not enforce triggers and constraint checks to fail if the user account lacks ALTER table permissions for the target table. |
This section has recommendations for to character mode (-c) and native mode (-n).
-
(Administrator/User) When possible, use native format (-n) to avoid the separator issue. Use the native format to export and import using SQL Server. Export data from SQL Server using the -c or -w option if the data will be imported to a non-SQL Server database.
-
(Administrator) Verify data when using BCP OUT. For example, when you use BCP OUT, BCP IN, and then BCP OUT verify that the data is properly exported and the terminator values are not used as part of some data value. Please consider overriding the default terminators (using -t and -r options) with random hexadecimal values to avoid conflicts between terminator values and data values.
-
(User) Use a long and unique terminator (any sequence of bytes or characters) to minimize the possibility of a conflict with the actual string value. This can be done by using the -t and -r options.
This section contains the following examples:
-
A. Copying table rows into a data file (with a trusted connection)
-
B. Copying table rows into a data file (with Mixed-mode Authentication)
-
C. Copying data from a file to a table
-
D. Copying a specific column into a data file
-
E. Copying a specific row into a data file
-
F. Copying data from a query to a data file
-
G. Creating a non-XML format file
-
H. Creating an XML format file
-
I. Using a format file to bulk import with bcp
A. Copying table rows into a data file (with a trusted connection)
The following example illustrates the out option on the AdventureWorks2012.Sales.Currency table. This example creates a data file named Currency.dat and copies the table data into it using character format. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At a command prompt, enter the following command:
bcp AdventureWorks2012.Sales.Currency out Currency.dat -T -c
B. Copying table rows into a data file (with mixed-mode authentication)
The following example illustrates the out option on the AdventureWorks2012.Sales.Currency table. This example creates a data file named Currency.dat and copies the table data into it using character format.
The example assumes that you are using mixed-mode authentication, you must use the -U switch to specify your login ID. Also, unless you are connecting to the default instance of SQL Server on the local computer, use the -S switch to specify the system name and, optionally, an instance name.
bcp AdventureWorks2012.Sales.Currency out Currency.dat -c -U<login_id> -S<server_name\instance_name>
The system will prompt you for your password.
C. Copying data from a file to a table
The following example illustrates the in option by using the file created in the preceding example (Currency.dat). First, however, this example creates an empty copy of the AdventureWorks2012 Sales.Currency table, Sales.Currency2, into which the data is copied. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
To create the empty table, in Query Editor, enter the following command:
USE AdventureWorks2012; GO SELECT * INTO AdventureWorks2012.Sales.Currency2 FROM AdventureWorks2012.Sales.Currency WHERE 1=2;
To bulk copy the character data into the new table, that is to import the data, enter the following command at a command prompt:
bcp AdventureWorks2012.Sales.Currency2 in Currency.dat -T -c
To verify that the command succeeded, display the contents of the table in Query Editor, and enter:
USE AdventureWorks2012; GO SELECT * FROM Sales.Currency2
D. Copying a specific column into a data file
To copy a specific column, you can use the queryout option. The following example copies only the Name column of the Sales.Currency table into a data file. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp "SELECT Name FROM AdventureWorks.Sales.Currency" queryout Currency.Name.dat -T -c
E. Copying a specific row into a data file
To copy a specific row, you can use the queryout option. The following example copies only the row for the contact named Jarrod Rana from the AdventureWorks2012.Person.Person table into a data file (Jarrod Rana.dat).The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp "SELECT * FROM AdventureWorks2012.Person.Person WHERE FirstName='Jarrod' AND LastName='Rana' " queryout "Jarrod Rana.dat" -T -c
F. Copying data from a query to a data file
To copy the result set from a Transact-SQL statement to a data file, use the queryout option. The following example copies the names from the AdventureWorks2012.Person.Person table, ordered by last name then first name, into the Contacts.txt data file. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp "SELECT FirstName, LastName FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname" queryout Contacts.txt -c -T
G. Creating a non-XML format file
The following example creates a non-XML format file, Currency.fmt, for the Sales.Currency table in the AdventureWorks2012 database. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp AdventureWorks2012.Sales.Currency format nul -T -c -f Currency.fmt
For more information, see Non-XML Format Files (SQL Server).
H. Creating an XML format file
The following example creates an XML format file named Currency.xml for the Sales.Currency table in the AdventureWorks2012 database. The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp AdventureWorks2012.Sales.Currency format nul -T -c -x -f Currency.xml
Note
|
|---|
|
To use the -x switch, you must be using a bcp 9.0 client. For information about how to use the bcp 9.0 client, see "Remarks." |
For more information, see XML Format Files (SQL Server).
I. Using a format file to bulk import with bcp
To use a previously created format file when importing data into an instance of SQL Server, use the -f switch with the in option. For example, the following command bulk copies the contents of a data file, Currency.dat, into a copy of the Sales.Currency table (Sales.Currency2) by using the previously created format file (Currency.xml). The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command.
At the Windows command prompt, enter:
bcp AdventureWorks2012.Sales.Currency2 in Currency.dat -T -f Currency.xml
Note
|
|---|
|
Format files are useful when the data file fields are different from the table columns; for example, in their number, ordering, or data types. For more information, see Format Files for Importing or Exporting Data (SQL Server). |
The following topics contain examples of using bcp:
