Understanding BCP

BULK COPY (BCP) offers a command-line alternative to the SQL Server Import And Export Wizard. The Transact-SQL counterpart to BCP import is BULK INSERT. You will find that BULK INSERT has a similar syntax when used for importing data. To learn more about BCP, we will examine its features:

  • Basics

  • Syntax

  • Permissions

  • Modes

  • Importing data

  • Exporting data

BCP Basics

BCP may continue to be a favorite of database administrators because of its great performance and minimal overhead. You will find that import and export processes tend to be very fast and that BCP requires very little memory to operate. BCP does not have a graphical user interface (GUI) and is best used in two situations:

  • To import data from a text file to a single SQL Server table or view

  • To export data to a text file from a single SQL Server table or view

When transferring data to or from SQL Server, BCP uses ODBC.

Tip

Tip

Dates are written in ODBC format. You will find that the datetime format is yyyymmdd hh:mm:ss rather than mmm dd yyy hh:mm (A.M./P.M.), and the money format has no commas with four digits after the decimal (instead of commas and two digits after the decimal).

Note

Note

When you import data using BCP, columns with computed values and time stamps are ignored. SQL Server can automatically assign values. To do this, use a format file to specify that the computed values or time-stamp columns in the table should be skipped; SQL Server then automatically assigns values for the column. During export, computed values and time stamps are handled like other values.

BCP Syntax

Before we examine how to use BCP, let’s look at the command syntax, shown in Example 10-1 and extended in Table 10-1 and Table 10-2. As you can see, the syntax is fairly extensive. BCP switches are case sensitive and order sensitive. You must use these switches exactly as indicated, or you will have problems executing the BCP command.

Example 10-1. BCP Syntax and Usage

Syntax

bcp {[[dbname.][owner].]{tablename | viewname } | "query"}
   {in | out | queryout | format} datafile      
   [switch1 [parameter1]] [switch2 [parameter2]]
   [switchN [parameterN]]

usage

bcp pubs..customer out customers.txt -c -U sa -P"guerilla"    
bcp pubs..customer in customers.txt -f customers.fmt -U sa -P"guerilla"

Table 10-1 provides a summary of key BCP parameters.

Table 10-1. Key Parameters Used with BCP

Parameter

Description

Dbname

The name of the database. This parameter is optional, and if it is not supplied, the user’s default database is used.

Owner

The schema (owner) of the table or view being used. Use the .. syntax for a default schema, such as pubs..authors instead of pubs.dbo.authors.

Tablename

The name of the table to access. Use the # or ## syntax to copy a temporary table.

Viewname

The name of the destination view when copying data into SQL Server; the source view when copying data from SQL Server.

Query

T-SQL statement that generates a result set. You must use double quotation marks around the query and specify the queryout parameter; these are mandatory with this option.

In

Specifies an import process.

Out

Specifies an export process.

Format

Sets the creation of a format file. You must set the name of the format file with the -f switch and also specify the format for this file with -n, -c, -w, -6, or -N. When creating an XML file, you must also specify -x.

Queryout

Must be used when exporting output from an SQL query or stored procedure.

Datafile

The name of the file for importing or the name of the file to create when exporting. This can include the full file path.

BCP also supports a wide variety of switches. These switches and their associated parameters are summarized in Table 10-2.

Table 10-2. Switches Used with BCP

Switch

Description

-a packetsize

Sets the number of bytes in a network packet. Default is 4,096 bytes. The valid range is 512 bytes to 65,535 bytes.

-b batchsize

The number of rows to transfer in the batch. Each batch is copied to the server as one transaction. By default, all rows are copied in a single batch. Do not use with the -h ROWS_PER_BATCH option.

-c

Character data mode (ASCII text) for transfers to and from non-SQL Server products.

-C codepage

Code page being used by the import file. This is only relevant when the data contains char, varchar, or text columns with character values greater than 127 or less than 32. Use the code page value ACP with ANSI ISO 1252 data, RAW when no conversion should occur, OEM to use the client’s default code page, or type a specific code page value, such as 850.

-e errfile

Stores error messages in the specified error file.

-E

Uses identity values. Otherwise, identity values are ignored and automatically assigned new values.

-F firstrow

Sets the number of the first row to use.

-f formatfile

Sets the name and path to a BCP format file. The default file name is BCP.FMT. If you use -n, -c, -w, -6, or -N and do not specify -f, you will be prompted for format information, and your responses will be saved in a format file (named BCP.FMT by default).

-h loadhints

Used to set load hints: FIRE_TRIGGERS, ROWS_PER_BATCH, KILOBYTES_PER_BATCH, TABLOCK, CHECK_CONSTRAINTS, and ORDER.

-I inputfile

Sets the name of a response file that contains responses to the command prompt questions for each field when performing a bulk copy using interactive mode.

-k

Preserves null values.

-L lastrow

Sets the last row to use.

-m maxerrors

Sets the maximum number of errors that can occur before terminating BCP. The default is 10.

-N

Sets native export for noncharacter data and Unicode character export for character data.

-n

Sets native data mode, which is SQL Server—specific.

-o outfile

File to redirect output of BCP during unattended operation.

-P password

Password to use to log on. Do not store passwords in files as this is poor security practice.

-q

Uses quoted identifiers.

-R

Enables regional format copy for currency, date, and time data.

-r rowterminator

Sets the row terminator. The default is the new line character ( ).

-S servername

Sets the SQL Server name. You can also follow the server name by the instance name: -S servernameinstancename.

-t fieldterminator

Sets the field terminator. The default is the tab character ( ).

-T

Uses a trusted connection, which is a good security practice.

-U username

Sets the user name for login.

-V

Sets the data type version for native and character formats to a previous SQL Server version. For SQL Server 6.0 format, use 60; for SQL Server 6.5, use 65; for SQL Server 7.0, use 70; for SQL Server 2000, use 80.

-v

Displays the BCP version number.

-w

Sets wide character (Unicode) mode.

-x

Used with the format and -f options to create an XML format file instead of a standard text-based format file.

BCP Permissions and Modes

Although any user can run BCP, only users with appropriate permissions can access SQL Server and the specified database objects. When you run BCP, you can set login information using the U and P switches, or you can use a trusted connection, which is more secure. For unattended operations, it is essential to use these switches to ensure that permissions are granted appropriately. To import data into a table, the user needs INSERT permission on the target table. To export data from a table, the user needs SELECT permission for the source table.

BCP can use three different modes:

  • Character mode. Used when you want to import or export data as ASCII text. The switch to set this mode is c.

  • Native mode. Used when you want to import or export data in native format. The switch to set this mode is n or N.

  • Wide mode. Used when you want to import or export data as Unicode text. The switch to set this mode is w.

The character and wide modes are the best choices when you are copying to a non-SQL Server product. Use native mode when you are copying data between SQL Server tables. These modes all have their strengths and weaknesses. With character or wide mode files, you can view the contents and make sure that you have the right data set, but for imports, you must also tell SQL Server how this data is formatted. You can do this through interactive prompts or by using a format file containing the responses to these prompts. With native mode, you cannot view the contents of native data files, but you do not have to specify data formatting information when importing files either.

Importing Data with BCP

You can import data with BCP in two ways. You can start an interactive session, or you can set the necessary responses in a format file. The following example shows how to start an interactive session using a trusted connection:

bcp pubs..customer in customers.txt -T

To specify a format file, use the -f flag, such as in the following example:

bcp pubs..customer in customers.txt -w -f customers.fmt -T

In an interactive session, BCP prompts you for information needed to complete the import or export process. BCP starts an interactive session when either of the following situations occurs:

  • You import without specifying the –c, –n, –w, or –N parameters.

  • You export without specifying the –c, –n, –w, or –N parameters.

The interactive session allows you to customize the BCP process, much as you do with a format file. In fact, before you try to create a format file, you should run BCP in interactive mode and then choose to have BCP create the necessary format file for you. This operation will show you the best way to configure the format file.

For each column in a table you are importing, you will see the prompts similar to the following during an interactive session:

Enter the file storage type of field [nchar]:
Enter prefix length of field [0]:
Enter length of field [5]:
Enter field terminator [none]:

Note

Note

Pressing Enter accepts the default values. To skip a column in an import file, type 0 for the prefix length, 0 for the field length, and none for the terminator type. You cannot skip a column when exporting data.

These prompts ask you to type various kinds of information, and in every case the default value for the current column is shown in brackets. At the end of the interactive session, you will be asked if you want to save your responses in a format file. If you answer yes (by typing Y), you can type the name of the format file when prompted, such as:

Do you want to save this format information in a file? [Y/N]
Host filename [bcp.fmt]: customers.fmt

You can then use the format file for other BCP sessions by setting the f switch as explained previously. Because the format file has a rigid syntax that you must follow, I recommend creating a sample file to get started. As Example 10-2 shows, each line in the file contains information fields that determine how data should be imported.

Example 10-2. BCP Non-XML Format File

9.0                                                     
50                                                      
1 SQLINT   0 8   ""  1  CUSTOMERID ""                   
2 SQLNCHAR 2 25  ""  2  CUSTNAME   SQL_Latin1_General_CP1_CI_AS 
3 SQLNCHAR 2 20  ""  3  CUSTORG    SQL_Latin1_General_CP1_CI_AS 
..                                                      
50 SQLNCHAR 2 9  ""  3  POSTALCODE  SQL_Latin1_General_CP1_CI_AS

The lines give you the following information:

  • The first line sets the version of BCP used. Here the version is 9.0.

  • The second line sets the number of columns in the table you are importing. In the example, the table contains 50 columns.

  • Subsequent lines set the formats for each column in the table, from the first column to the last column.

The lines defining table columns are broken down into fields, and each field sets a different input parameter. Normally, these fields are separated by spaces. The number of spaces does not really matter—provided there is at least one space. BCP treats one or more spaces as a field separator. File format fields operate in the following manner:

  • Field 1 sets the column number you are describing from the data file.

  • Field 2 sets the file storage type, which is simply the data type of the column.

  • Field 3 sets the prefix length for compacted data. A value of zero specifies that no prefix is used.

  • Field 4 sets the field length, which is the number of bytes required to store the data type. Use the default value provided whenever possible.

  • Field 5 sets the field terminator. By default, BCP separates all fields but the last one with tabs ( ) and separates the last field with a carriage return and new-line field ( ).

  • Field 6 sets the table column number in the database. For example, a value of 1 means that the column corresponds to the first column in the database.

  • Field 7 sets the table column name.

  • Field 8 sets the column collation.

Exporting Data with BCP

When you export data, BCP creates a data file using the name you specify. If you are exporting data from nonnative files (ASCII and Unicode text), the columns in this file are separated with tabs by default, and the last column has a carriage return and newline. You specify a tab as a terminator with and a carriage return and newline with . In a format file, a tab can be an actual tab character or a series of five or more spaces.

As when importing data, you can handle data export interactively. For example, if you start an export session without specifying format information, you are prompted for this information. In the following example, you export a table to a file called customers.txt and use semicolons as the delimiter:

bcp pubs..customer out customers.txt -c -t -T;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset