Uredi

Deli z drugimi prek


bcp utility

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL database in Microsoft Fabric

The bulk copy program utility (bcp) bulk copies data between an instance of SQL Server and a data file in a user-specified format.

Use the bcp utility 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 more information about which version of bcp is installed on your system, system requirements, and how to get bcp, see Download and install the bcp utility.

Note

bcp data files don't include any schema or format information. If you use bcp to back up data, and then later drop or alter the source table, you need either an identical table definition or a format file to import the data back.

For the syntax conventions that are used for the bcp syntax, see Transact-SQL syntax conventions (Transact-SQL).

How to use bcp

For information about how to use bcp, including example commands, see How to use the bcp utility.

Use bcp on Linux and macOS

For information on how to install the command-line tools on macOS and Linux, see Install the sqlcmd and bcp SQL Server command-line tools on Linux.

Considerations for bcp on Linux and macOS

  • The field terminator is a tab (\t).

  • The line terminator is a newline (\n).

  • For SQL Server to SQL Server transfers, use native format (-n). Use character format (-c) only when the data crosses into a non-SQL Server system or when the data file shouldn't contain extended characters. For more information, see Character mode and native mode best practices.

  • You must quote or escape a backslash (\) on a command-line argument. For example, to specify a newline as a custom row terminator, use one of the following mechanisms:

    • -r\\n
    • -r"\n"
    • -r'\n'

TDS 8.0 support

SQL Server 2025 (17.x) introduces TDS 8.0 support for the bcp utility.

Syntax

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 ]
    [ -D ]
    [ -e err_file ]
    [ -E ]
    [ -f format_file ]
    [ -F first_row ]
    [ -G Microsoft Entra authentication ]
    [ -h"hint [ , ...n ] " ]
    [ -i input_file ]
    [ -k ]
    [ -K application_intent ]
    [ -l login_timeout ]
    [ -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 ]
    [ -u ]
    [ -v ]
    [ -V (80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 | 160 | 170) ]
    [ -w ]
    [ -x ]
    [ -Y [ s | m | o ] ]
    [ -z ]

Command-line options

The following table lists the command-line options available in bcp, and which operating systems they support.

Command-line option Supported on Windows Supported on Linux and macOS
Object and transfer mode
[database_name.] schema.{table_name | view_name | "query"} Yes Yes
{in data_file | out data_file | queryout data_file | format nul} Yes Yes
-q Yes Yes
Connection and authentication
-S [server_name[\instance_name]] Yes Yes
-d database_name Yes Yes
-U login_id Yes Yes
-P password Yes Yes
-G Microsoft Entra authentication Yes Yes
-D Yes Yes
-K application_intent Yes Yes
-l login_timeout Yes Yes
-T Yes Yes
-Y[s| m| o] Yes 1 Yes 1
Data representation
-c Yes Yes
-C { ACP | OEM | RAW | code_page } Yes No
-n Yes Yes
-N Yes No
-w Yes Yes
-z No Yes 2
Format files
-f format_file Yes Yes
-x Yes No
Batching and performance
-a packet_size Yes Yes
-b batch_size Yes Yes
-h"hint [,...n]" Yes No
-m max_errors Yes Yes
-F first_row Yes Yes
-L last_row Yes Yes
-r row_term Yes Yes
-t field_term Yes Yes
Value handling
-k Yes Yes
-E Yes Yes
File I/O and logging
-i input_file Yes No
-o output_file Yes No
-e err_file Yes Yes
Compatibility and versioning
-V (80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 | 160 | 170 ) Yes No
-u Yes 1 Yes 1
Miscellaneous options
-R Yes Yes
-v Yes Yes

1 Requires bcp version 18 or later, which ships with SQL Server 2025 (17.x).
2 ODBC 18.6.1.1 and later versions.

Object and transfer mode

database_name

The name of the database that contains the specified table or view. If you don't specify this parameter, the default database for the user is used.

You can also explicitly specify the database name with -d.

schema

The name of the owner of the table or view. schema is optional if the user performing the operation owns the specified table or view. If you don't specify schema, and the user performing the operation doesn't own the specified table or view, SQL Server returns an error message, and the operation is canceled.

table_name

The name of the destination table when importing data into SQL Server (in), and the source table when exporting data from SQL Server (out).

view_name

The name of the destination view when copying data into SQL Server (in), and the source view when copying data from SQL Server (out). When used as a destination (in), a view is only supported if all of its columns refer to the same table. This restriction doesn't apply when the view is used as a source (out). For more information on the restrictions for copying data into views, see INSERT.

"query"

A Transact-SQL query that returns a result set. If the query returns multiple result sets, only the first result set is copied to the data file; subsequent result sets are ignored. Use double quotation marks around the query and single quotation marks around anything embedded in the query. You must also specify queryout when bulk copying data from a query.

The query can reference a stored procedure as long as all tables referenced inside the stored procedure exist before executing the bcp statement. For example, if the stored procedure generates a temp table, the bcp statement fails because the temp table is available only at run time and not at statement execution time. In this case, consider inserting the results of the stored procedure into a table and then use bcp to copy the data from the table into a data file.

in

Copies data from a file into the database table or view. Specifies the direction of the bulk copy.

out

Copies data from the database table or view to a file. Specifies the direction of the bulk copy.

If you specify an existing file, the file is overwritten. When the bcp utility extracts data, it represents an empty string as a null, and a null string as an empty string.

data_file

The full path of the data file. When you bulk import data into SQL Server, the data file contains the data to copy into the specified table or view. When you bulk export data from SQL Server, the data file contains the data copied from the table or view. The path can have from 1 through 255 characters. The data file can contain a maximum of 2^63 - 1 rows.

queryout

Copies data from a query and must be specified only when bulk copying data from a query.

format

Creates a format file based on the option specified (-n, -c, -w, or -N) and the table or view delimiters. When bulk copying data, the bcp command can refer to a format file, which saves you from reentering format information interactively. The format option requires the -f option; creating an XML format file also requires the -x option. For more information, see Create a format file with bcp (SQL Server). You must specify nul as the value (format nul).

-q

Executes the SET QUOTED_IDENTIFIER ON statement in the connection between the bcp utility and an instance of SQL Server. Use this option to specify a database, owner, table, or view name that contains a space or a single quotation mark. Enclose the entire three-part table or view name in quotation marks ("").

To specify a database name that contains a space or single quotation mark, you must use the -q option.

-q doesn't apply to values passed to -d.

For more information, see the Remarks section in this article.

Connection and authentication

-S [server_name[\instance_name]]

Specifies the name of the SQL Server instance to connect to, or if you use -D, a DSN.

If you don't specify a server, the bcp utility connects to the default instance of SQL Server on the local computer. You need this option when running a bcp command from a remote computer on the network or a local named instance. To connect to the default instance of SQL Server on a server, specify only server_name. To connect to a named instance of SQL Server, specify <server_name>\<instance_name>.

-d database_name

Specifies the database to connect to. By default, bcp connects to your default database. If you specify -d <database_name> and a three-part name (database_name.schema.table, passed as the first parameter to bcp), an error occurs because you can't specify the database name twice. If database_name begins with a hyphen (-) or a forward slash (/), don't add a space between -d and the database name.

-U login_id

Specifies the login ID used to connect to SQL Server.

-P password

Specifies the password for the login ID. If you don't use this option, the bcp command prompts for a password. If you use this option at the end of the command prompt without a password, bcp uses the default password (NULL).

Important

Do not use a blank password. Use a strong password.

To mask your password, don't specify the -P option along with the -U option. Instead, after specifying bcp along with the -U option and other switches (don't specify -P), press the Enter key, and the command prompts you for a password. This method ensures that your password is masked when it's entered.

If password begins with a hyphen (-) or a forward slash (/), don't add a space between -P and the password value.

On Linux and macOS, when used with the -G option without -U, -P specifies a file that contains a Microsoft Entra access token (v17.8 and later versions). The token file should be in UTF-16LE (no BOM) format. For more information, see Authenticate with Microsoft Entra ID in bcp.

-G

Applies to: Azure SQL Database, Azure SQL Managed Instance, SQL database in Microsoft Fabric, Azure Synapse Analytics, and SQL Server 2022 (16.x) and later versions.

This switch is used by the client to specify that the user is authenticated with Microsoft Entra ID. The -G switch requires bcp version 14.0.3008.27 or later versions. To determine your version, execute bcp -v. For more information, see Use Microsoft Entra authentication with SQL Database or Azure Synapse Analytics or Authentication in SQL database in Microsoft Fabric.

For full details about Microsoft Entra authentication in bcp, see Authenticate with Microsoft Entra ID in bcp.

-D

Causes the value passed to the bcp -S option to be interpreted as a data source name (DSN). -D can appear anywhere on the command line; ordering relative to -S doesn't matter.

A DSN can be used to:

  • Embed driver options to simplify command lines.
  • Enforce driver options that aren't otherwise accessible from the command line, such as MultiSubnetFailover.
  • Help protect sensitive credentials from being discoverable as command-line arguments.

For more information, see DSN support in sqlcmd and bcp.

-K application_intent

Declares the application workload type when connecting to a server. The only value that is possible is ReadOnly. If you don't specify -K, the bcp utility doesn't support connectivity to a secondary replica in an Always On availability group. For more information, see Offload read-only workload to secondary replica of an Always On availability group.

-l login_timeout

Specifies a login timeout. The -l option specifies the number of seconds before a login to SQL Server times out when you try to connect to a server. The default login timeout is 15 seconds. The login timeout must be a number between 0 and 65534. If the value you supply isn't numeric or doesn't fall into that range, bcp generates an error message. A value of 0 specifies an infinite timeout.

-T

Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. The security credentials of the network user, login_id, and password aren't required. If you don't specify -T, you need to specify -U and -P to successfully connect.

Important

Use -T only when connecting to SQL Server with a Windows-integrated trusted connection. When connecting to Azure SQL Database or Azure Synapse Analytics, -T (Windows integrated authentication) isn't supported. For Microsoft Entra authentication against Azure services or SQL Server 2022 (16.x) and later versions, use -G (see Authenticate with Microsoft Entra ID in bcp).

-Y[s|m|o]

Applies to: bcp version 18 and later versions, which ships with SQL Server 2025 (17.x).

Specifies whether connections use TLS encryption over the network. -Y can be o (for Optional), m (for Mandatory, the default), or s (for Strict). If you don't include -Y, then -Ym (for Mandatory) is the default.

-u

Applies to: bcp version 18 and later versions, which ships with SQL Server 2025 (17.x).

Trust server certificate. When used with the encryption option for the connection, enables encryption using a self-signed server certificate.

Data representation

-c

Performs the operation using a character data type. This option doesn't prompt for each field. It uses char as the storage type, without prefixes, and uses \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c isn't compatible with -w.

For more information, see Use character format to import or export data (SQL Server).

For best practices, see Character mode and native mode best practices.

-C { ACP | OEM | RAW | code_page }

Applies to: Windows only. Not supported on Linux and macOS.

Specifies the code page of the data in the data file. code_page is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32.

Specify a collation name for each column in a format file, except when you want the 65001 option to have priority over the collation or code page specification.

Code page value Description
ACP ANSI/Microsoft Windows (ISO 1252).
OEM Default code page used by the client. This code page is the default if you don't specify -C.
RAW No conversion from one code page to another occurs. This option is the fastest because no conversion occurs.
<code_page> Specific code page number, such as 850.

Versions before SQL Server 2016 (13.x) don't support code page 65001 (UTF-8 encoding). Versions beginning with 13 can import UTF-8 encoding to earlier versions of SQL Server.

-n

Performs the bulk-copy operation using the native (database) data types of the data. This option doesn't prompt for each field. It uses the native values.

For more information, see Use native format to import or export data (SQL Server).

For best practices, see Character mode and native mode best practices.

-N

Applies to: Windows only. Not supported on Linux and macOS.

Performs the bulk-copy operation using the native (database) data types of the data for noncharacter data, and Unicode characters for character data. This option offers a higher performance alternative to the -w option, and is intended for transferring data from one instance of SQL Server to another using a data file. It doesn't prompt for each field. Use this option when you're transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode.

For more information, see Use Unicode Native Format to Import or Export Data (SQL Server).

If you export and then import data to the same table schema by using bcp with -N, you might see a truncation warning if there's a fixed length, non-Unicode character column (for example, char(10)).

You can ignore the warning. One way to resolve this warning is to use -n instead of -N.

-w

Performs the bulk copy operation by using Unicode characters. This option doesn't prompt for each field. It uses nchar as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator. -w isn't compatible with -c.

For more information, see Use Unicode character format to import or export data (SQL Server).

-z

Applies to: bcp (ODBC), Linux and macOS only. Windows isn't supported.

Enables vector data type support in the bcp utility. This feature is currently disabled by default. When disabled, vector data is imported or exported as JSON float array strings. When enabled, and when connecting to SQL Server 2025 (17.x) and later versions, vector data is imported or exported in native vector binary.

Format files

-f format_file

Specifies the full path of a format file. The meaning of this option depends on the environment in which it's used, as follows:

  • If you use -f with the format option, the specified format_file is created for the specified table or view. To create an XML format file, also specify the -x option. For more information, see Create a format file with bcp (SQL Server).

  • If you use -f with the in or out option, it requires an existing format file.

    Note

    Using a format file with the in or out option is optional. If you don't specify the -f option and you don't specify -n, -c, -w, or -N, the command prompts for format information and lets you save your responses in a format file. The default file name is bcp.fmt.

If format_file begins with a hyphen (-) or a forward slash (/), don't include a space between -f and the format_file value.

-x

Applies to: Windows only. Not supported on Linux and macOS.

Use this option with the format and -f format_file options. It generates an XML-based format file instead of the default non-XML format file. The -x option doesn't work when importing or exporting data. It generates an error if used without both format and -f format_file.

Batching and performance

-a packet_size

Specifies the number of bytes per network packet that the client sends to and receives from the server. Set this server configuration option by using SQL Server Management Studio or the sp_configure system stored procedure. However, you can override the server configuration option on an individual basis by using this option. packet_size can be from 4,096 bytes to 65,535 bytes. The default is 4096.

Increasing the packet size can improve the performance of bulk-copy operations. If you request a larger packet but the server can't grant it, the default is used. The performance statistics that the bcp utility generates show the packet size used.

-b batch_size

Specifies the number of rows per batch of imported data. Each batch is imported and logged as a separate transaction that imports the whole batch before being committed. By default, bcp imports all the rows in the data file as one batch. To distribute the rows among multiple batches, specify a batch_size that is smaller than the number of rows in the data file. If the transaction for any batch fails, only insertions from the current batch are rolled back. Batches already imported by committed transactions aren't affected by a later failure.

The -b and the -h "ROWS_PER_BATCH=<bb>" hint are mutually exclusive. Use -b when you want bcp to control batching explicitly, or use ROWS_PER_BATCH to hint the server optimizer when sending the data as a single transaction.

-h "hints [, ... n]"

Applies to: Windows only. Not supported on Linux and macOS.

Specifies the hint or hints to use during a bulk import of data into a table or view.

  • ORDER (column [ASC | DESC] [, ...n])

    The sort order of the data in the data file. Bulk import performance improves if the data being imported is sorted according to the clustered index on the table, if any. If the data file is sorted in a different order, that is, other than the order of a clustered index key, or if there's no clustered index on the table, the ORDER clause is ignored. The column names you supply must be valid column names in the destination table. By default, bcp assumes the data file is unordered. For optimized bulk import, SQL Server also validates that the imported data is sorted.

  • ROWS_PER_BATCH = bb

    Number of rows of data per batch (as bb). Used when you don't specify -b, resulting in the entire data file being sent to the server as a single transaction. The server optimizes the bulkload according to the value bb. By default, ROWS_PER_BATCH is unknown.

  • KILOBYTES_PER_BATCH = cc

    Approximate number of kilobytes of data per batch (as cc). By default, KILOBYTES_PER_BATCH is unknown.

  • TABLOCK

    Specifies that a bulk update table-level lock is acquired during the bulkload operation; otherwise, a row-level lock is acquired. This hint significantly improves performance because holding a lock during the bulk-copy operation reduces lock contention on the table. You can load a table concurrently from multiple clients if the table has no indexes and TABLOCK is specified. By default, the table option table lock on bulkload determines locking behavior. For more information, see sp_tableoption.

    Note

    If the target table is clustered columnstore index, TABLOCK hint isn't required for loading by multiple concurrent clients because each concurrent thread is assigned a separate rowgroup within the index and loads data into it. For more information, see Columnstore indexes: overview.

  • CHECK_CONSTRAINTS

    Specifies that all constraints on the target table or view must be checked during the bulk-import operation. Without the CHECK_CONSTRAINTS hint, any CHECK and FOREIGN KEY constraints are ignored, and after the operation the constraint on the table is marked as not-trusted.

    Note

    UNIQUE, PRIMARY KEY, and NOT NULL constraints are always enforced.

    At some point, you need to check the constraints on the entire table. If the table was nonempty before the bulk import operation, the cost of revalidating the constraint can exceed the cost of applying CHECK constraints to the incremental data. Therefore, you can usually enable constraint checking during an incremental bulk import.

    A situation in which you might want constraints disabled (the default behavior) is if the input data contains rows that violate constraints. By disabling CHECK constraints, you can import the data and then use Transact-SQL statements to remove data that isn't valid.

    Note

    The -m max_errors switch doesn't apply to constraint checking.

  • FIRE_TRIGGERS

    When you specify this option with the in argument, any insert triggers defined on the destination table run during the bulk-copy operation. If you don't specify FIRE_TRIGGERS, no insert triggers run. FIRE_TRIGGERS is ignored for the out, queryout, and format arguments.

-m max_errors

Specifies the maximum number of syntax errors that can occur before the bcp operation is canceled. A syntax error implies a data conversion error to the target data type. The max_errors total excludes any errors that the server can detect only, such as constraint violations.

A row that the bcp utility can't copy is ignored and counted as one error. If you don't include this option, the default value is 10.

Note

The -m option doesn't apply when converting the money or bigint data types.

-F first_row

Specifies the number of the first row to export from a table or import from a data file. This parameter requires a value greater than (>) 0 but less than (<) or equal to the total number of rows. If you don't specify this parameter, the default is the first row of the file.

first_row can be a positive integer with a value up to 2^63-1. -F first_row uses 1-based numbering.

-L last_row

Specifies the number of the last row to export from a table or import from a data file. This parameter requires a value greater than (>) 0 but less than (<) or equal to the number of the last row. If you don't specify this parameter, the default is the last row of the file.

last_row can be a positive integer with a value up to 2^63-1.

-r row_term

Specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator. For more information, see Specify field and row terminators (SQL Server).

If you specify the row terminator in hexadecimal notation in a bcp command, the value is truncated at 0x00. For example, if you specify 0x410041, 0x41 is used.

If row_term begins with a hyphen (-) or a forward slash (/), don't include a space between -r and the row_term value.

-t field_term

Specifies the field terminator. The default is \t (tab character). Use this parameter to override the default field terminator. For more information, see Specify field and row terminators (SQL Server).

If you specify the field terminator in hexadecimal notation in a bcp command, the value is truncated at 0x00. For example, if you specify 0x410041, 0x41 is used.

If field_term begins with a hyphen (-) or a forward slash (/), don't include a space between -t and the field_term value.

Value handling

-k

Specifies that empty columns keep a null value during the operation, rather than inserting any default values for the columns. For more information, see Keep nulls or default values during bulk import (SQL Server).

-E

Specifies that the operation uses identity values in the imported data file for the identity column. If you don't specify -E, SQL Server ignores the identity values for this column in the data file being imported and automatically assigns unique values based on the seed and increment values specified during table creation. For more information, see DBCC CHECKIDENT.

If the data file doesn't contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data. SQL Server automatically assigns unique values for the column.

The -E option has a special permissions requirement. For more information, see "Remarks" later in this article.

File I/O and logging

-i input_file

Applies to: Windows only. Not supported on Linux and macOS.

Specifies the name of a response file. The file contains the responses to the command prompt questions for each data field when you perform a bulk copy operation using interactive mode (-n, -c, -w, or -N not specified).

If input_file begins with a hyphen (-) or a forward slash (/), don't include a space between -i and the input_file value.

-o output_file

Applies to: Windows only. Not supported on Linux and macOS.

Specifies the name of a file that receives output redirected from the command prompt.

If output_file begins with a hyphen (-) or a forward slash (/), don't include a space between -o and the output_file value.

-e err_file

Specifies the full path of an error file used to store any rows that the bcp utility can't transfer from the file to the database. Error messages from the bcp command go to the workstation of the user. If you don't use this option, an error file isn't created.

If err_file begins with a hyphen (-) or a forward slash (/), don't include a space between -e and the err_file value.

Compatibility and versioning

-V { 80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 | 160 | 170 }

Applies to: Windows only. Not supported on Linux and macOS.

Performs the bulk-copy operation using data types from an earlier version of SQL Server. This option doesn't prompt for each field; it uses the default values.

  • 80 = SQL Server 2000 (8.x)
  • 90 = SQL Server 2005 (9.x)
  • 100 = SQL Server 2008 (10.0.x) and SQL Server 2008 R2 (10.50.x)
  • 110 = SQL Server 2012 (11.x)
  • 120 = SQL Server 2014 (12.x)
  • 130 = SQL Server 2016 (13.x)
  • 140 = SQL Server 2017 (14.x)
  • 150 = SQL Server 2019 (15.x)
  • 160 = SQL Server 2022 (16.x)
  • 170 = SQL Server 2025 (17.x)

For example, to generate data for types that SQL Server 2000 (8.x) doesn't support, but were introduced in later versions, use the -V80 option.

For more information, see Import native and character format data from earlier versions of SQL Server.

Miscellaneous options

-R

Specifies that the bcp utility bulk copies currency, date, and time data into SQL Server using the regional format defined for the locale setting of the client computer. By default, bcp ignores regional settings.

-v

Reports the bcp utility version number and copyright.

Remarks

  • The bcp utility supports native data files that are compatible with all supported versions of SQL Server.

  • The bcp utility displays only the first 512 bytes of an error message.

Permissions

A bcp out operation requires SELECT permission on the source table.

A bcp in operation minimally requires SELECT and INSERT permissions on the target table. In addition, ALTER TABLE permission is required if any of the following conditions are true:

  • Constraints exist and the CHECK_CONSTRAINTS hint isn't specified.

    Disabling constraints is the default behavior. To enable constraints explicitly, use the -h option with the CHECK_CONSTRAINTS hint.

  • Triggers exist and the FIRE_TRIGGERS hint isn't specified.

    By default, triggers aren't 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.

Get help

Contribute to SQL documentation

Did you know that you can edit SQL content yourself? If you do so, not only do you help improve our documentation, but you also get credited as a contributor to the page.

For more information, see Edit Microsoft Learn documentation.