Bulk Copy

advertisement
SQL Server 2000 Notes
Chapter 21
Page 1 of 11
Chapter 21: The Bulk Copy Program (bcp)
bcp (and BULK INSERT) Fundamentals
 bcp is a command-line program for getting a lot of data into or out of SQL Server quickly.
 BULK INSERT is a T-SQL command that uses bcp to import large amounts of data; it can not be used
to export data.
bcp Syntax
bcp {[[database_name.][owner].]{table_name | view_name} | "query"}
{in | out | queryout | format} data_file
[-m max_errors] [-f format_file] [-e err_file]
[-F first_row] [-L last_row] [-b batch_size]
[-n] [-c] [-w] [-N] [-V (60 | 65 | 70)] [-6]
[-q] [-C code_page] [-t field_term] [-r row_term]
[-i input_file] [-o output_file] [-a packet_size]
[-S server_name[\instance_name]] [-U login_id] [-P password]
[-T] [-v] [-R] [-k] [-E] [-h "hint [,...n]"]

database_name - Is the name of the database in which the specified table or view resides. If not
specified, this is the default database for the user.

Owner - Is the name of the owner of the table or view.
o owner is optional if the user performing the bulk copy operation owns the specified table or
view.
o If owner is not specified and the user performing the bulk copy operation does not own the
specified table or view, Microsoft® SQL Server™ 2000 returns an error message, and the bulk
copy operation is canceled.

table_name - Is the name of the destination table when copying data into SQL Server (in), and the
source table when copying data from SQL Server (out).

view_name - Is the name of the destination view when copying data into SQL Server (in), and the
source view when copying data from SQL Server (out).
o Only views in which all columns refer to the same table can be used as destination views.

Query - Is a Transact-SQL query that returns a result set.
o If the query returns multiple result sets, such as a SELECT statement that specifies a COMPUTE
clause, only the first result set is copied to the data file; subsequent result sets are ignored.
o Use double quotation marks around the query and single quotation marks around anything
embedded in the query.
o queryout must also be specified when bulk copying data from a query.
SQL Server 2000 Notes
Chapter 21
Page 2 of 11

in | out | queryout | format - Specifies the direction of the bulk copy.
o in copies from a file into the database table or view.
o out copies from the database table or view to a file.
o queryout must be specified only when bulk copying data from a query.
o format creates a format file based on the option specified (-n, -c, -w, -6, or -N) and the table or
view delimiters.
o If format is used, the -f option must be specified as well.
o Note: The bcp utility included with Microsoft SQL Server 6.5 does not support bulk copying into
tables that contain the sql_variant or bigint data types.

data_file - Is the full path of the data file used when bulk copying a table or view to or from a disk.
o When bulk copying data into SQL Server, the data file contains the data to be copied into the
specified table or view.
o When bulk copying 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.

-m max_errors - Specifies the maximum number of errors that can occur before the bulk copy
operation is canceled.
o Each row that cannot be copied by bcp is ignored and counted as one error.
o The default is 10.

-f format_file - Specifies the full path of the format file that contains stored responses from a previous
use of bcp on the same table or view.
o Use this option when using a format file created with the format option to bulk copy data in or
out.
o Creation of the format file is optional.
o After prompting you with format questions, bcp prompts whether to save the answers in a format
file.
o The default file name is Bcp.fmt. bcp can refer to a format file when bulk copying data;
therefore, reentering previous format responses interactively is not necessary.
o If this option is not used and -n, -c, -w, -6, or -N is not specified, bcp prompts for format
information.

-e err_file - Specifies the full path of an error file used to store any rows bcp is unable to transfer from
the file to the database.
o Error messages from bcp go to the user's workstation.
o If this option is not used, an error file is not created.

-F first_row - Specifies the number of the first row to bulk copy.
o The default is 1, indicating the first row in the specified data file.

-L last_row - Specifies the number of the last row to bulk copy.
o The default is 0, indicating the last row in the specified data file.

-b batch_size - Specifies the number of rows per batch of data copied.
o Each batch is copied to the server as one transaction.
SQL Server 2000 Notes
Chapter 21
Page 3 of 11
o SQL Server commits or rolls back, in the case of failure, the transaction for every batch.
o By default, all data in the specified data file is copied in one batch.
o Do not use in conjunction with the -h "ROWS_PER_BATCH = bb" option.

-n - Performs the bulk copy operation using the native (database) data types of the data. This option does
not prompt for each field; it uses the native values.

-c - Performs the bulk copy operation using a character data type. This option does not prompt for each
field; it uses char as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline
character) as the row terminator.

-w - Performs the bulk copy operation using Unicode characters. This option does not 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. Cannot be used with SQL Server version 6.5 or earlier.

-N - Performs the bulk copy operation using the native (database) data types of the data for noncharacter
data, and Unicode characters for character data.
o This option offers a higher performance alternative to the -w option.
o This option is intended for transferring data from one SQL Server to another using a data file.
o It does not prompt for each field.
o Use this option when you are transferring data that contains ANSI extended characters and you
want to take advantage of the performance of native mode.
o -N cannot be used with SQL Server 6.5 or earlier.

-V (60 | 65 | 70) - Performs the bulk copy operation using data types from an earlier version of SQL
Server.
o Use this option in conjunction with character (-c) or native (-n) format.
o This option does not prompt for each field; it uses the default values.
o For example, to bulk copy date formats supported by the bcp utility provided with SQL Server
6.5 (but no longer supported by ODBC) into SQL Server 2000, use the -V 65 parameter.

-6 - Performs the bulk copy operation using SQL Server 6.0 or SQL Server 6.5 data types.
o Supported for backward compatibility only.
o Use the -V option instead.

-q - Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp
utility and an instance of SQL Server.
o When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation
marks, and literals must be delimited by single quotation marks. When SET
QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL
rules for identifiers.
o Use this option to specify a database, owner, table, or view name that contains a space or a
quotation mark.
o Enclose the entire three-part table or view name in double quotation marks (" ").

-C code_page - Specifies the code page of the data in the data file.
SQL Server 2000 Notes
Chapter 21
Page 4 of 11
o Supported for backward compatibility only.
o Instead, specify a collation name for each column in the format file or in interactive bcp.
o code_page is relevant only if the data contains char, varchar, or text columns with character
values greater than 127 or less than 32.

-t field_term - Specifies the field terminator.
o The default is \t (tab character).
o Use this parameter to override the default field terminator.

-r row_term - Specifies the row terminator.
o The default is \n (newline character).
o Use this parameter to override the default row terminator.

-i input_file - Specifies the name of a response file, containing the responses to the command prompt
questions for each field when performing a bulk copy using interactive mode (-n, -c, -w, -6, or -N not
specified).

-o output_file - Specifies the name of a file that receives output from bcp redirected from the command
prompt.

-a packet_size - Specifies the number of bytes, per network packet, sent to and from the server.
o A server configuration option can be set by using SQL Server Enterprise Manager (or the
sp_configure system stored procedure).
o The server configuration option can be overridden on an individual basis by using this option.
o packet_size can be from 4096 to 65535 bytes; the default is 4096.
o Increased packet size can enhance performance of bulk copy operations. If a larger packet is
requested but cannot be granted, the default is used. The performance statistics generated by bcp
show the packet size used.

-S server_name[\instance_name] - Specifies the instance of SQL Server to connect to.
o Specify server_name to connect to the default instance of SQL Server on that server.
o Specify server_name\instance_name to connect to a named instance of SQL Server 2000 on that
server.
o If no server is specified, bcp connects to the default instance of SQL Server on the local
computer.
o This option is required when executing bcp from a remote computer on the network.

-U login_id - Specifies the login ID used to connect to SQL Server.

-P password - Specifies the password for the login ID.
o If this option is not used, bcp prompts for a password.
o If this option is used at the end of the command prompt without a password, bcp uses the default
password (NULL).

-T - Specifies that bcp connects to SQL Server with a trusted connection, using the security credentials
of the network user. login_id and password are not required.
SQL Server 2000 Notes
Chapter 21
Page 5 of 11

-v - Reports the bcp utility version number and copyright.

-R - Specifies that currency, date, and time data is bulk copied into SQL Server using the regional
format defined for the locale setting of the client computer. By default, regional settings are ignored.

-k - Specifies that empty columns should retain a null value during the bulk copy operation, rather than
have any default values for the columns inserted.

-E - Specifies that the values for an identity column are present in the file being imported.
o If -E is not given, the identity values for this column in the data file being imported are ignored,
and SQL Server 2000 automatically assigns unique values based on the seed and increment
values specified during table creation.
o If the data file does not 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 2000 automatically assigns unique values for the column.

-h "hint [,...n]" - Specifies the hint(s) to be used during a bulk copy of data into a table or view.
o This option cannot be used when bulk copying data into SQL Server 6.x or earlier.
o Hint Description:
 ORDER (column [ASC | DESC] [,...n]) Sort order of the data in the data file. Bulk copy
performance is improved if the data being loaded is sorted according to the clustered
index on the table. If the data file is sorted in a different order, or there is no clustered
index on the table, the ORDER hint is ignored. The names of the columns supplied must
be valid columns in the destination table. By default, bcp assumes the data file is
unordered.
 ROWS_PER_BATCH = bb Number of rows of data per batch (as bb). Used when -b is
not specified, resulting in the entire data file being sent to the server as a single
transaction. The server optimizes the bulk load according to the value bb. By default,
ROWS_PER_BATCH is unknown.
 KILOBYTES_PER_BATCH = cc Approximate number of kilobytes (KB) of data per
batch (as cc). By default, KILOBYTES_PER_BATCH is unknown.
 TABLOCK A table-level lock is acquired for the duration of the bulk copy operation.
This hint significantly improves performance because holding a lock only for the duration
of the bulk copy operation reduces lock contention on the table. A table can be loaded
concurrently by multiple clients if the table has no indexes and TABLOCK is specified.
By default, locking behavior is determined by the table option table lock on bulk load.
 CHECK_CONSTRAINTS Any constraints on the destination table are checked during
the bulk copy operation. By default, constraints are ignored.
 FIRE_TRIGGERS Specified with the in argument, any insert triggers defined on the
destination table will execute during the bulk copy operation. If FIRE_TRIGGERS is not
specified, no insert triggers will execute. FIRE_TRIGGERS is ignored for the out,
queryout, and format arguments.
SQL Server 2000 Notes
Chapter 21
Page 6 of 11
Remarks:
 Values in the data file being imported for computed or timestamp columns are ignored, and SQL Server
2000 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.

SQL Server identifiers, including database names, table or view names, logins, and passwords, can
include characters such as embedded spaces and quotation marks. When you specify an identifier or file
name at the command prompt that includes a space or quotation mark, enclose the identifier in double
quotation marks (" "). Additionally, 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 of the double quotation marks.

To specify a database name that contains a space or quotation mark, you must use the –q option.
Examples:
 Suppose the Northwind database has the table Jane's Orders, which is owned by user Jane Doe. To bulk
copy this table from the Northwind database to the Orders.txt file using the login Jane Doe and the
password go dba, execute one of these commands:
o bcp "Northwind.Jane Doe.Jane's Orders" out "Jane's Orders.txt" -c -q -U"Jane Doe" -P"go dba"
o bcp "Northwind.[Jane Doe].[Jane's Orders]" out "Jane's Orders.txt" -c -U"Jane Doe" -P"go dba"
SQL Server 2000 Notes

Chapter 21
Page 7 of 11
Create some data in Excel for the Employees table in Northwind, then copy it into SQL Server:
o Create a spreadsheet like below.
EmployeeID
1
2
3
LastName
Mirus
Lehnen
Roberts
FirstName
Kevin
Al
Kooky
o Save the spreadsheet as a tab-delimited text file called Names.txt
o Get the format information for the Employees table using:
 bcp Northwind.dbo.Employees format c:\Employees.fmt -fE.fmt -oEmpInfo.txt -t\n -N r\n -ST264_95642_15\NETSDK –P
o Adjust E.fmt to:
8.0
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SQLINT
0
4
SQLNCHAR
2
SQLNCHAR
2
SQLNCHAR
0
SQLNCHAR
0
SQLDATETIME 0
SQLDATETIME 0
SQLNCHAR
0
SQLNCHAR
0
SQLNCHAR
0
SQLNCHAR
0
SQLNCHAR
0
SQLNCHAR
0
SQLNCHAR
0
SQLIMAGE
0
SQLNCHAR
0
SQLINT
0
0
SQLNCHAR
0
"\r\n"
40
"\r\n"
20
"\r\n"
0 "\r\n"
0 "\r\n"
0
"\r\n"
0
"\r\n"
0 "\r\n"
0 "\r\n"
0 "\r\n"
0 "\r\n"
0 "\r\n"
0 "\r\n"
0
"\r\n"
0
"\r\n"
0
"\r\n"
"\r\n"
0 "\r\n"
1
EmployeeID
""
2 LastName
SQL_Latin1_General_CP1_CI_AS
3 FirstName
SQL_Latin1_General_CP1_CI_AS
0 Title
SQL_Latin1_General_CP1_CI_AS
0 TitleOfCourtesy SQL_Latin1_General_CP1_CI_AS
0 BirthDate
""
0 HireDate
""
0 Address
SQL_Latin1_General_CP1_CI_AS
0 City
SQL_Latin1_General_CP1_CI_AS
0 Region
SQL_Latin1_General_CP1_CI_AS
0 PostalCode
SQL_Latin1_General_CP1_CI_AS
0 Country
SQL_Latin1_General_CP1_CI_AS
0 HomePhone
SQL_Latin1_General_CP1_CI_AS
0 Extension
SQL_Latin1_General_CP1_CI_AS
0 Photo
""
0 Notes
SQL_Latin1_General_CP1_CI_AS
0 ReportsTo
""
0 PhotoPath
SQL_Latin1_General_CP1_CI_AS
o Load data using: bcp Northwind.dbo.Employees in c:\Names.txt -fE.fmt -oEmpInfoLoad.txt -t\n
-N -r\n -ST264_95642_15\NETSDK -P
SQL Server 2000 Notes
Chapter 21
Page 8 of 11
BULK INSERT Syntax:
BULK INSERT [ [ 'database_name'.] [ 'owner' ].] { 'table_name' FROM 'data_file' }
[ WITH
(
[ BATCHSIZE [ = batch_size ] ]
[ [ , ] CHECK_CONSTRAINTS ]
[ [ , ] CODEPAGE [ = 'ACP' | 'OEM' | 'RAW' | 'code_page' ] ]
[ [ , ] DATAFILETYPE [ =
{ 'char' | 'native'| 'widechar' | 'widenative' } ] ]
[ [ , ] FIELDTERMINATOR [ = 'field_terminator' ] ]
[ [ , ] FIRSTROW [ = first_row ] ]
[ [ , ] FIRE_TRIGGERS ]
[ [ , ] FORMATFILE = 'format_file_path' ]
[ [ , ] KEEPIDENTITY ]
[ [ , ] KEEPNULLS ]
[ [ , ] KILOBYTES_PER_BATCH [ = kilobytes_per_batch ] ]
[ [ , ] LASTROW [ = last_row ] ]
[ [ , ] MAXERRORS [ = max_errors ] ]
[ [ , ] ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) ]
[ [ , ] ROWS_PER_BATCH [ = rows_per_batch ] ]
[ [ , ] ROWTERMINATOR [ = 'row_terminator' ] ]
[ [ , ] TABLOCK ]
)
]

'database_name' - Is the database name in which the specified table or view resides. If not specified,
this is the current database.

'owner' - Is the name of the table or view owner. owner is optional if the user performing the bulk copy
operation owns the specified table or view. If owner is not specified and the user performing the bulk
copy operation does not own the specified table or view, Microsoft® SQL Server™ returns an error
message, and the bulk copy operation is canceled.

'table_name' - Is the name of the table or view to bulk copy data into. Only views in which all columns
refer to the same base table can be used. For more information about the restrictions for copying data
into views, see INSERT.

'data_file' - Is the full path of the data file that contains data to copy into the specified table or view.
BULK INSERT can copy data from a disk (including network, floppy disk, hard disk, and so on).

BATCHSIZE [ = batch_size ] - Specifies the number of rows in a batch. Each batch is copied to the
server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for
every batch. By default, all data in the specified data file is one batch.

CHECK_CONSTRAINTS - Specifies that any constraints on table_name are checked during the bulk
copy operation. By default, constraints are ignored.
SQL Server 2000 Notes
Chapter 21
Page 9 of 11

CODEPAGE [ = 'ACP' | 'OEM' | 'RAW' | 'code_page' ] - Specifies the code page of the data in the
data file.
o CODEPAGE is relevant only if the data contains char, varchar, or text columns with character
values greater than 127 or less than 32.
o CODEPAGE value Description o ACP Columns of char, varchar, or text data type are converted from the ANSI/Microsoft
Windows® code page (ISO 1252) to the SQL Server code page.
o OEM (default) Columns of char, varchar, or text data type are converted from the system OEM
code page to the SQL Server code page.
o RAW No conversion from one code page to another occurs; this is the fastest option.
o code_page Specific code page number, for example, 850.

DATAFILETYPE [ = {'char' | 'native' | 'widechar' | 'widenative' } ] - Specifies that BULK INSERT
performs the copy operation using the specified default.
o DATAFILETYPE value Description
o char (default) Performs the bulk copy operation from a data file containing character data.
o native Performs the bulk copy operation using the native (database) data types. The data file to
load is created by bulk copying data from SQL Server using the bcp utility.
o widechar Performs the bulk copy operation from a data file containing Unicode characters.
o widenative Performs the same bulk copy operation as native, except char, varchar, and text
columns are stored as Unicode in the data file. The data file to be loaded was created by bulk
copying data from SQL Server using the bcp utility. This option offers a higher performance
alternative to the widechar option, and is intended for transferring data from one computer
running SQL Server to another by using a data file. Use this option when transferring data that
contains ANSI extended characters in order to take advantage of native mode performance.

FIELDTERMINATOR [ = 'field_terminator' ] - Specifies the field terminator to be used for char and
widechar data files. The default is \t (tab character).

FIRSTROW [ = first_row ] - Specifies the number of the first row to copy. The default is 1, indicating
the first row in the specified data file.

FIRE_TRIGGERS - Specifies that any insert triggers defined on the destination table will execute
during the bulk copy operation.
o If FIRE_TRIGGERS is not specified, no insert triggers will execute.

FORMATFILE [ = 'format_file_path' ] - Specifies the full path of a format file. A format file
describes the data file that contains stored responses created using the bcp utility on the same table or
view. The format file should be used in cases in which:
o The data file contains greater or fewer columns than the table or view.
o The columns are in a different order.
o The column delimiters vary.
o There are other changes in the data format. Format files are usually created by using the bcp
utility and modified with a text editor as needed.
SQL Server 2000 Notes
Chapter 21
Page 10 of 11

KEEPIDENTITY - Specifies that the values for an identity column are present in the file imported. If
KEEPIDENTITY is not given, the identity values for this column in the data file imported are ignored,
and SQL Server automatically assigns unique values based on the seed and increment values specified
during table creation. If the data file does not 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.

KEEPNULLS - Specifies that empty columns should retain a null value during the bulk copy operation,
rather than have any default values for the columns inserted.

KILOBYTES_PER_BATCH [ = kilobytes_per_batch ] - Specifies the approximate number of
kilobytes (KB) of data per batch (as kilobytes_per_batch). By default, KILOBYTES_PER_BATCH is
unknown.

LASTROW [ = last_row ] - Specifies the number of the last row to copy. The default is 0, indicating
the last row in the specified data file.

MAXERRORS [ = max_errors ] - Specifies the maximum number of errors that can occur before the
bulk copy operation is canceled. Each row that cannot be imported by the bulk copy operation is ignored
and counted as one error. If max_errors is not specified, the default is 10.

ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) - Specifies how the data in the data file is sorted. Bulk
copy operation performance is improved if the data loaded is sorted according to the clustered index on
the table. If the data file is sorted in a different order, or there is no clustered index on the table, the
ORDER clause is ignored. The column names supplied must be valid columns in the destination table.
By default, the bulk insert operation assumes the data file is unordered.
o N - Is a placeholder indicating that multiple columns can be specified.

ROWS_PER_BATCH [ = rows_per_batch ] - Specifies the number of rows of data per batch (as
rows_per_batch). Used when BATCHSIZE is not specified, resulting in the entire data file sent to the
server as a single transaction. The server optimizes the bulk load according to rows_per_batch. By
default, ROWS_PER_BATCH is unknown.

ROWTERMINATOR [ = 'row_terminator' ] - Specifies the row terminator to be used for char and
widechar data files. The default is \n (newline character).

TABLOCK - Specifies that a table-level lock is acquired for the duration of the bulk copy operation. A
table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is
specified. By default, locking behavior is determined by the table option table lock on bulk load.
Holding a lock only for the duration of the bulk copy operation reduces lock contention on the table,
significantly improving performance.

Remarks - The BULK INSERT statement can be executed within a user-defined transaction. Rolling
back a user-defined transaction that uses a BULK INSERT statement and BATCHSIZE clause to load
data into a table or view using multiple batches rolls back all batches sent to SQL Server.
SQL Server 2000 Notes

Chapter 21
Page 11 of 11
Permissions - Only members of the sysadmin and bulkadmin fixed server roles can execute BULK
INSERT.
Examples
This example imports order detail information from the specified data file using a pipe (|) as the field terminator
and |\n as the row terminator.
BULK INSERT Northwind.dbo.[Order Details]
FROM 'f:\orders\lineitem.tbl'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '|\n'
)
This example specifies the FIRE_TRIGGERS argument.
BULK INSERT Northwind.dbo.[Order Details]
FROM 'f:\orders\lineitem.tbl'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = ':\n',
FIRE_TRIGGERS
)
Download