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] [-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]
[-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 own 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

is the name of the database table or view to copy.

in | out

is the direction of the copy. in indicates a copy from a file into the database table, and out indicates a copy to a file from the database table.

datafile

is the name of an operating system file.

-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

is 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 as the storage type of all columns in the data file. This option does not prompt for each field; it uses char as the storage type, no prefixes, \t (tab) as the default field terminator, and \n (newline) as the default row terminator.

-e errfile

is the 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. This is only effective when copying data into a table. bcp reads the value of the ID column from the data file, but ignores it and does not send it to the server; instead, 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 will read the value from the data file and send it to the server, which will insert these values into the table. If the number of inserted rows exceeds the maximum possible IDENTITY column value, Adaptive Server returns an error.

The -E option has no effect when copying data out, in other words, the ID column is copied to the data file (unless the -N option is used).

-f formatfile

is 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 when 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

is 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 located in 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 sets character-set conversion to NULL. 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 which may not necessarily be the character set that the client is using. Table A-1 lists platform defaults.

Table A-1: Default character sets for different platforms

Platform

Default character set

Sun, Digital, Pyramid, RS6000/AIX, others

iso_1

HP

roman8

-K keytab_file

can be 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

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

-m maxerrors

is 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 usually not human-readable.

WARNING! Do not use bcp in native format to recover data, salvage data, or resolve an emergency situation. Do not use bcp in native format to transport data between different hardware platforms, different operating systems, or different major versions 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), the data will be unrecoverable.

-N

skips the IDENTITY column. Use this option when copying data in if your host data file does not include a place holder for the IDENTITY column values, or when copying data out and you do not want to include the IDENTITY column information in the host file.

You cannot use both -N and -E options when copying in data.

-P password

specifies an Adaptive Server password. This option is ignored if -V is used.

-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. -q in conjunction with -J specifies the character set translation file (.xlt file) required for the conversion.

In Japanese language environments, the -q flag translates Hankaku Katakana (half-width characters) into Zenkaku Katakana (full-width characters). Use with the argument “zenkaku” and with the -J flag to indicate the client’s Japanese character set (sjis or eucjis). The zenkaku.xlt file was designed to translate only from terminal display to Adaptive Server, not from Adaptive Server to the terminal.

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

r – Enable data replay detection

q – Enable out-of-sequence detection

-v

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

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

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

-z language

is 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 afterwards 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 $SYBASE/install/libtcl.cfg configuration file. 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 UNIX.

Examples

Example 1

  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. The first backslash before the final “r” escapes the second so that one backslash prints.

    bcp pubs2..publishers out pub_out -c -t , -r \\r
    
  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 field terminator [none]: 
    
     
    
     Enter the file storage type of field city [char]: 
    
     Enter prefix length of field city [1]: 
    
     Enter field terminator [none]: 
    
     
    
     Enter the file storage type of field state [char]: 
    
     Enter prefix length of field state [1]: 
    
     Enter field terminator [none]: 
    
     
    
     Do you want to save this format information in a
    
    file? [Y] y 
    
     Host filename [bcp.fmt]: pub_form 
    
     
    
     Starting copy... 
    
     
    
     3 rows copied. 
    
     Clock Time (ms.): total = 300   Avg = 1 (300.00 rows
    
     per sec.) 
    
  3. To copy this data back into Adaptive Server using the saved format file, pub_form, use the following command:

    bcp pubs2..publishers in pub_out -f pub_form 
    
  4. To see a list of possible datatypes, enter “?” at the prompt:

    Enter the file storage type of field ’pub_id’
    
     [’char’]:?
    
     Invalid column type. Valid types are:
    
     <cr>: same type as Adaptive Server column.
    
         c : char 
    
         T : text 
    
         i : int 
    
         s : smallint 
    
         t : tinyint 
    
         f : float 
    
         m : money 
    
         b : bit 
    
         d : datetime 
    
         x : binary 
    
         I : image 
    
         D : smalldatetime 
    
         r : real 
    
         M : smallmoney 
    
         n : numeric 
    
         e : decimal 
    

    Enter the single letter exactly as it appears above.

  5. The following example copies a data file created with a character set used on a VT200 terminal into the pubs2.publishers table. The -q flag translates it. The -z flag displays bcp messages in French.

    bcp pubs2..publishers in vt200_data -J iso_1 -q vt200 -z french
    
  6. The following example specifies that Adaptive Server send 40K of text or image data using a packet size of 4096:

    bcp publishers out -T 40960 -A 4096
    

Usage


New features

NoteIf there is an external Sybase configuration file, add this section to enable bcp: [BCP]

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 first 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; re-create the indexes and triggers; and then dump the database. Remember to allocate disk space for the construction of indexes and triggers for a clustered index, about 1.2 times the amount of space needed for the data, in addition to the 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.


Messages

Error in attempting to load a view of translation tables.

The character translation file(s) named with the -a or -q parameter is missing, or you mistyped the name(s).