Copies a database table to or from an operating system file in a user-specified format.
bcp [[database_name.]owner.]table_name {in | out} datafile [-c] [-E] [-n] [-v] [-X] [-a display_charset] [-A size] [-b batchsize] [-e errfile] [-f formatfile] [-F firstrow] [-I interfaces_file] [-J client_charset] [-K keytab_file] [-L lastrow] [-m maxerrors] [-P password] [-q datafile_charset] [-r row_terminator] [-R remote_server_principal] [-S server] [-t field_terminator] [-T text_or_image_size] [-U username] [-V [security_options [-Y ] [-z language] [-Z security_mechanism]
Is optional if the table being copied is in your default database or in master. Otherwise, you must specify a database name.
Is optional if you or the Database Owner owns the table being copied. If you do not specify an owner, bcp looks first for a table of that name owned by you. Then it looks for one owned by the Database Owner. If another user owns the table, you must specify the owner name or the command fails.
The name of the database table or view to copy.
The direction of the copy. in indicates a copy from a file into the database table, while out indicates a copy to a file from the database table.
The full path name of an operating system file. The path name can be from 1 to 255 characters in length.
Allows you to run bcp from a terminal where the character set differs from that of the machine on which bcp is running. -a in conjunction with -J specifies the character set translation file (.xlt file) required for the conversion. Use -a without -J only if the client character set is the same as the default character set.
Specifies the network packet size to use for this bcp session. For example:
bcp -A 2048
sets the packet size to 2048 bytes for this bcp session. size must be between the values of the default network packet size and maximum network packet size configuration variables, and it must be a multiple of 512.
Use larger-than-default network packet sizes to improve the performance of large bulk-copy operations.
The number of rows per batch of data copied (the default is to copy all the rows in one batch). Batching applies only when bulk copying in; it has no effect on bulk copying out.
Performs the copy operation with char datatype as the default. This option does not prompt for each field; it uses char as the default storage type, no prefixes, \t (tab) as the default field terminator, and \n (newline) as the default row terminator.
The full path name of an error file where bcp stores any rows that it was unable to transfer from the file to the database. Error messages from the bcp program appear on your terminal. bcp creates an error file only when you specify this option.
Explicitly specifies the value of a table’s IDENTITY column.
By default, when you bulk copy data into a table with an IDENTITY column, bcp assigns each row a temporary IDENTITY column value of 0. As bcp inserts each row into the table, the server assigns the row a unique, sequential IDENTITY column value, beginning with the value 1. If you specify the -E flag when copying data into a table, bcp prompts you to enter an explicit IDENTITY column value for each row. If the number of inserted rows exceeds the maximum possible IDENTITY column value, Adaptive Server returns an error.
By default, when you bulk copy data from a table with an IDENTITY column, bcp excludes all information about the column from the output file. If you specify the -E flag, bcp copies the existing IDENTITY column values into the output file.
The full path name of a file with stored responses from a previous use of bcp on the same table. After you answer bcp’s format questions, it prompts you to save your answers in a format file. Creation of the format file is optional. The default file name is bcp.fmt. The bcp program can refer to a format file when copying data, so that you do not have to duplicate your previous format responses interactively. Use this option only if you previously created a format file that you want to use now for a copy in or out. If this option is not used, bcp queries you for format information interactively.
The number of the first row to copy (default is the first row).
Specifies the name and location of the interfaces file to search when connecting to Adaptive Server. If you do not specify -I, bcp looks for an interfaces file (sql.ini on Windows platforms) located in the ini directory, which is below the directory specified by the SYBASE environment variable.
Specifies the character set to use on the client. bcp uses a filter to convert input between client_charset and the Adaptive Server character set.
-J client_charset requests that Adaptive Server convert to and from client_charset, the character set used on the client.
With no argument disables character set conversion. No conversion takes place. Use this if the client and server use the same character set.
Omitting -J sets the character set to a default for the platform.
The default may not necessarily be the character set that the client is using. (See the System Administration Guide for more information about character sets and associated flags.
Used only with DCE security. It specifies a DCE keytab file that contains the security key for the user name specified with -U option. Keytab files can be created with the DCE dcecp utility. See your DCE documentation for more information.
If the -K option is not supplied, the bcp user must be logged in to DCE with the same user name as specified with the -U option.
The number of the last row to copy (default is the last row).
The maximum number of errors permitted before bcp aborts the copy. bcp throws out each row that it cannot build, counting it as one error. If you do not include this option, bcp uses a default value of 10.
Performs the copy operation using native (operating system) formats. This option does not prompt for each field. Files in native data format are not human-readable.
WARNING! Do not use bcp in native format to perform data recovery, salvage, or to resolve an emergency situation. Do not use bcp in native format to transport data between different hardware platforms, different operating systems, or different major releases of Adaptive Server. Using bcp in native format can create flat files that cannot be reloaded into Adaptive Server and it may be impossible to recover the data. If you are unable to rerun bcp in character format (for example, table truncated/dropped, hardware damage, database dropped, and so on) the data will be unrecoverable.
Specifies an Adaptive Server password. If you do not specify -P password, bcp prompts for a password. If your password is NULL, place the -P flag at the end of the command line by itself.
Allows you to run bcp to copy character data to or from a file system that uses a character set different from the client character set.
In Japanese language environments, the -q flag translates Hankaku Katakana (half-width characters) into Zenkaku Katakana (full-width characters).
The ascii_7 character set is compatible with
all character sets. If either Adaptive Server’s or the
client’s character set is set to ascii_7, any
7-bit ASCII character is allowed to pass between client and server
unaltered. Other characters produce conversion errors. Character
set conversion issues are covered more thoroughly in the System
Administration Guide.
Specifies the default row terminator.
Specifies the principal name for the server. By default, a server’s principal name matches the server’s network name (which is specified with the -S option or the DSQUERY environment variable). The -R option must be used when the server’s principal name and network name are not the same.
Specifies the name of the Adaptive Server to connect to. If you specify -S with no argument, bcp uses the server that your DSQUERY environment value specifies.
Specifies the default field terminator.
Allows you to specify, in bytes, the maximum length of text or image data that Adaptive Server sends. The default is 32K. If a text or image field is larger than the value of -T or the default, bcp does not send the overflow.
Specifies an Adaptive Server login name. If you do not specify username, bcp uses the current user’s operating system login name.
Reports the current version and copyright message of the bcp program.
Specifies network-based user authentication. With this option, the user must log in to the network’s security system before running the utility. In this case, users must supply their network user name with the -U option; any password supplied with the -P option is ignored.
-V can be followed by a security_options string of key-letter options to enable additional security services. These key letters are:
c – Enable data confidentiality service
i – Enable data integrity service
m – Enable mutual authentication for connection establishment
o – Enable data origin stamping service
q – Enable out-of-sequence detection
r – Enable data replay detection
Specifies that, in this connection to the server, the application initiate the login with client-side password encryption. bcp (the client) specifies to the server that password encryption is desired. The server sends back an encryption key, which bcp uses to encrypt your password, and the server uses the key to authenticate your password when it arrives.
Specifies that the character-set conversion is disabled in the server, and is performed by bcp on the client side when using bcp IN. Enables client-side conversion.
All character-set conversion is done in the server during bcp
OUT.
The official name of an alternate language that the server uses to display bcp prompts and messages. Without the -z flag, bcp uses the server’s default language. Add languages to an Adaptive Server at installation or afterward with the utility langinstall or the stored procedure sp_addlanguage.
Specifies the name of a security mechanism to use on the connection.
Security mechanism names are defined in the libtcl.cfg configuration file which is located in the ini subdirectory below the Sybase installation directory. If no security_mechanism name is supplied, the default mechanism is used.
For more information on security mechanism names, see the description of the libtcl.cfg file in the Open Client and Open Server Configuration Guide for Microsoft Windows.
In the following example, the -c option copies data out of the publishers table in character format (using char for all fields). The -t field_terminator option ends each field with a comma, and the -r row_terminator option ends each line with a Return. bcp prompts only for a password.
bcp pubs2..publishers out pub_out -c -t ,
-r \r
In the following example, bcp copies data from the publishers table to a file named pub_out for later reloading into Adaptive Server. Pressing Return accepts the defaults that the prompts specify. The same prompts appear when copying data into the publishers table.
bcp pubs2..publishers out pub_out
Password:
Enter the file storage type of field pub_id [char]:
Enter prefix length of field pub_id [0]:
Enter length of field pub_id [4]:
Enter field terminator [none]:
Enter the file storage type of field pub_name [char]:
Enter prefix length of field pub_name [1]:
Enter length of field pub_name [40]:
Enter field terminator [none]:
Enter the file storage type of field city [char]:
Enter prefix length of field city [1]:
Enter length of field city [20]:
Enter field terminator [none]:
Enter the file storage type of field state [char]:
Enter prefix length of field state [1]:
Enter length of field state [2]:
Enter field terminator [none]:
In the following example, the -c option copies data out of the publishers table in character format (using char for all fields). The -t field_terminator option ends each field with a comma, and the -r row_terminator option ends each line with a Return. bcp prompts only for a password.
bcp pubs2..publishers out pub_out -c -t ,
-r \r
In the following example, bcp copies data from the publishers table to a file named pub_out for later reloading into Adaptive Server. Pressing Return accepts the defaults specified by the prompts. The same prompts appear when copying data into the publishers table.
bcp pubs2..publishers out pub_out
Password:
Enter the file storage type of field pub_id [char]:
Enter prefix length of field pub_id [0]:
Enter length of field pub_id [4]:
Enter field terminator [none]:
Enter the file storage type of field pub_name [char]:
Enter prefix length of field pub_name [1]:
Enter length of field pub_name [40]:
Enter field terminator [none]:
Enter the file storage type of field city [char]:
Enter prefix length of field city [1]:
Enter length of field city [20]:
Enter field terminator [none]:
Enter the file storage type of field state [char]:
Enter prefix length of field state [1]:
Enter length of field state [2]:
Enter field terminator [none]:
bcp provides a convenient and high-speed method for transferring data between a database table or view and an operating system file. It is capable of reading or writing files in a wide variety of formats. When copying in from a file, bcp inserts data into an existing database table; when copying out to a file, bcp overwrites any previous contents of the file.
Upon completion, bcp informs you of the number of rows of data successfully copied, the total time the copy took, the average amount of time in milliseconds that it took to copy one row, and the number of rows copied per second.
bcp for System 11 is built with Client-Library. The bcp user interface is unchanged except for the following:
New command-line options have been added to enable network-based security services on the connection as follows:
-Kkeytab_file -Rremote_server_principal -Vsecurity_options -Z security_mechanism
The -y sybase_directory option is ignored.
Error message format is different than previous versions of bcp. If you have scripts that perform routines based on the values of these messages you may need to rewrite them, for example:
The display message that indicates the number of rows transferred has been changed. During a session, this version of bcp periodically reports a running total of rows transferred. This message replaces the “1000 rows transferred” message displayed by the previous bcp.
To use a previous version of bcp,
you must set the CS_BEHAVIOR property in the [bcp] section
of the ocs.cfg file:
[bcp]
CS_BEHAVIOR = CS_BEHAVIOR_100
If CS_BEHAVIOR is not set to CS_BEHAVIOR_100, you can use functionality for bcp 11.1 and later.
The bcp program is optimized to load data into tables that do not have indexes or triggers associated with them. It loads data into tables without indexes or triggers at the fastest possible speed, with a minimum of logging. Page allocations are logged, but the insertion of rows is not.
When you copy data into a table that has one or more indexes or triggers, a slower version of bcp is automatically used, which logs row inserts. This includes indexes implicitly created using the unique integrity constraint of a create table statement. However, bcp does not enforce the other integrity constraints defined for a table.
Because the fast version of bcp inserts data without logging it, the System Administrator or Database Owner must first set the system procedure sp_dboption, “DB”, true. If the option is not true, and you try to copy data into a table that has no indexes or triggers, Adaptive Server generates an error message. You do not need to set this option in order to copy data out to a file, or in order to copy data into a table that contains indexes or triggers.
Because bcp logs inserts into a table
that has indexes or triggers, the log can grow very large. You can
truncate the log with dump transaction after
the bulk copy completes and after you have backed up your database with dump
database.
While the select into/bulkcopy option is on, you are not allowed to dump the transaction log. Issuing dump transaction produces an error message instructing you to use dump database instead.
WARNING! Be certain that you dump your database before you turn off the select into/bulkcopy flag. If you have inserted unlogged data into your database, and you then perform a dump transaction before performing a dump database, you will not be able to recover your data.
Unlogged bcp runs more slowly while a dump database is taking place.
Table A-1 shows which version bcp uses when copying in, the necessary settings for the select into/bulkcopy option, and whether the transaction log is kept and dumpable.
select into/ bulkcopy on |
select into/ bulkcopy off |
|
---|---|---|
Fast bcp (no indexes or triggers on target table) |
OK dump transaction prohibited |
bcp dump transaction prohibited |
Slow bcp (one or more indexes or triggers) |
OK dump transaction prohibited |
OK dump transaction OK |
By default, the select into/bulkcopy option is off in newly created databases. To change the default situation, turn this option on in the model database.
The performance penalty for copying data into a table
that has indexes or triggers in place can be severe. If you are
copying in a very large number of rows, it may be faster to drop
all the indexes and triggers beforehand with drop index (or alter
table for indexes created as a unique constraint) and drop
trigger; set the database option; copy the data into the
table; recreate the indexes and triggers; and then dump the database.
However, you need to allocate disk space for the construction of
indexes and triggers—about 2.2 times the amount of space
needed for the data.
When you copy data in or out using the -n (native format) or -c (character format) option, bcp only prompts you for your password, unless you supplied it with the -P option. If you do not supply either the -n, -c or -f formatfile option, bcp prompts you for information for each field in the table.
Each prompt displays a default value, in brackets, which you can accept by pressing Return. The prompts include:
The file storage type, which can be character or any valid Adaptive Server datatype
The prefix length, which is an integer indicating the length in bytes of the following data
The storage length of the data in the file for non NULL fields
The field terminator, which can be any character string
Scale and precision for numeric and decimal datatypes
The row terminator is the field terminator of the last field in the table or file.
The bracketed defaults represent reasonable values for the datatypes of the field in question. For the most efficient use of space when copying out to a file:
Use the default prompts
Copy all data in their table datatypes
Use prefixes as indicated
Do not use terminators
Accept the default lengths
Table A-2 shows the defaults and possible alternate responses:
Prompt |
Default provided |
Possible responses |
---|---|---|
File storage type |
Use database storage type for most fields except: char for varchar binary for varbinary |
char to create or read a human-readable file; any Adaptive Server datatype where implicit conversion is supported. |
Prefix length |
|
0 if no prefix is desired; defaults are recommended in all other cases. |
Storage length |
For char and varchar, use defined length. For binary and varbinary saved as char, use default. For all other datatypes, use maximum length needed to avoid truncation or data overflow. |
Default values, or greater, are recommended. |
Field or row terminator |
None |
Up to 30 characters, or one of the following: \t tab \n newline \r carriage return \0 null terminator \ backslash |
bcp can copy data out to a file either as its native (database) datatype, or as any datatype for which implicit conversion is supported for the datatype in question. bcp copies user-defined datatypes as their base datatype or as any datatype for which implicit conversion is supported. For more information on datatype conversions, see dbconvert in the Open Client DB-Library/C Reference Manual.
Be careful when you copy data from different releases
of Adaptive Server, because not all releases have the same datatypes.
A prefix length is a 1-byte, 2-byte, or 4-byte integer that represents the length of each data value in bytes. It immediately precedes the data value in the host file.
Fields defined in the database as char, nchar, and binary are always padded with spaces (null bytes for binary) to the full length defined in the database. timestamp data is treated as binary(8).
If data in varchar and varbinary fields is longer than the length you specify for copy out, bcp silently truncates the data in the file at the specified length.
A field terminator string can be up to 30 characters long. The most common terminators are a tab (entered as “\t” and used for all columns except the last one), and a newline (entered as “\n” and used for the last field in a row). Other terminators are: “\0” (the null terminator), “\” (backslash), and “\r” (Return). When choosing a terminator, be sure that its pattern does not appear in any of your character data. For example, if you use tab terminators with a string that contains a tab, bcp can not identify which tab represents the end of the string. Since bcp always looks for the first possible terminator, in this case it will find the wrong one.
When a terminator or prefix is present, it affects the actual length of data transferred. If the length of an entry being copied out to a file is less than the storage length, it is followed immediately by the terminator, or the prefix for the next field. The entry is not padded to the full storage length (char, nchar, and binary data is returned from Adaptive Server already padded to the full length).
When copying in from a file, data is transferred until either the number of bytes indicated in the “Length” prompt has been copied or the terminator is encountered. Once a number of bytes equal to the specified length has been transferred, the rest of the data is flushed until the terminator is encountered. When no terminator is used, the table storage length is strictly observed.
The following tables show the interaction of prefix lengths, terminators, and field length on the information in the file. “P” indicates the prefix in the stored table; “T” indicates the terminator; and dashes, “--”, show appended spaces. “...” indicates that the pattern repeats for each field. The field length is 8 for each column, and “string” represents the 6-character field each time.
Prefix length = 0 |
Prefix length 1, 2 or 4 |
|
---|---|---|
No terminator |
string--string-- |
Pstring--Pstring-- |
Terminator |
string--Tstring--T |
Pstring--TPstring--T |
Prefix length = 0 |
Prefix length 1, 2 or 4 |
|
---|---|---|
No terminator |
string--string-- |
PstringPstring |
Terminator |
stringTstringT |
PstringTPstringT |
Note that the file storage type and length of a column do not have to be the same as the type and length of the column in the database table. (If types and formats copied in are incompatible with the structure of the database table, the copy fails.)
File storage length generally indicates the maximum amount of data to be transferred for the column, excluding terminators and/or prefixes.
When copying data into a table, bcp observes any defaults defined for columns and user-defined datatypes. However, bcp ignores rules in order to load data at the fastest possible speed.
Because bcp considers any data column that can contain null values to be variable length, use either a length prefix or terminator to denote the length of each row of data.
Data written to a host file in its native format preserves all of its precision. datetime and float values preserve all of their precision even when they are converted to character format. Adaptive Server stores money values to a precision of one ten-thousandth of a monetary unit. However, when money values are converted to character format, their character format values are recorded only to the nearest two places.
Before copying data that is in character format from a file into a database table, check the datatype entry rules in the “Datatypes” section of the Adaptive Server Reference Manual. Character data that is being copied into the database with bcp must conform to those rules. Note especially that dates in the undelimited (yy)yymmdd format may result in overflow errors if the year is not specified first.
When you send host data files to sites that use terminals different from your own, inform them of the datafile_charset that you used to create the files.
Error in attempting to load a view of translation tables.
The character translation file(s) named with the -q parameter is missing, or you mistyped the name(s).