bcp

Description

Copies a database table to or from an operating system file in a user-specified format.

Syntax

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]

Parameters

database_name

Is optional if the table being copied is in your default database or in master. Otherwise, you must specify a database name.

owner

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.

table_name

The name of the database table or view to copy.

in | out

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.

datafile

The full path name of an operating system file. The path name can be from 1 to 255 characters in length.

-a display_charset

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.

-A size

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.

-b batchsize

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.

-c

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.

-e errfile

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.

-E

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.

-f formatfile

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.

-F firstrow

The number of the first row to copy (default is the first row).

-I interfaces_file

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.

-J client_charset

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.

-J

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.

-K keytab_file

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.

-L lastrow

The number of the last row to copy (default is the last row).

-m maxerrors

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.

-n

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.

-P password

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.

-q datafile_charset

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).

NoteThe 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.

-r row_terminator

Specifies the default row terminator.

-R remote_server_principal

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.

-S server

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.

-t field_terminator

Specifies the default field terminator.

-T text_or_image_size

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.

-U username

Specifies an Adaptive Server login name. If you do not specify username, bcp uses the current user’s operating system login name.

-v

Reports the current version and copyright message of the bcp program.

-V security_options

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

-X

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.

-Y

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.

NoteAll character-set conversion is done in the server during bcp OUT.

-z language

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.

-Z security_mechanism

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.

Examples

Example 1

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

Example 2

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]: 

Example 3

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

Example 4

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]: 

Usage


New features

bcp for System 11 is built with Client-Library. The bcp user interface is unchanged except for the following:

NoteTo 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.


Copying tables with indexes or triggers

NoteThe 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.


Responding to bcp prompts

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.

Table A-3: Adaptive Server char data

Prefix length = 0

Prefix length 1, 2 or 4

No terminator

string--string--

Pstring--Pstring--

Terminator

string--Tstring--T

Pstring--TPstring--T

Table A-4: Other datatypes converted to char storage

Prefix length = 0

Prefix length 1, 2 or 4

No terminator

string--string--

PstringPstring

Terminator

stringTstringT

PstringTPstringT


Messages

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).