usage of bcp command.(快来看)。
2006-04-14 23:13:11 来源:WEB开发网核心提示:这个是我从Manual上copy下来的,希望对大家有用,usage of bcp command.(快来看)。,bcpDescription Copiesa database to or from an operating system file in a user-specified format.Syntax bc
这个是我从Manual上copy下来的,希望对大家有用。
bcp
Description
Copies a database to or from an operating system file in a user-specified format.
Syntax
bcp [[database_name.]owner.][view_name | table_name [:partition_id]] {in | out} datafile[-c] [-E] [-n] [-X]
[-a display_charset]
[-A size]
[-b batchsize]
[-e errfile]
[-f formatfile]
[-F firstrow]
[-g id_start_value]
[-I interfaces_file]
[-J client_charset]
[-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]
[-z language]
or
bcp -v
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 that you own, and then looks for one owned by the Database Owner. If another user owns the table, you must specify the owner name or the command fails.
view_name
is the name of the view you are copying out.
table_name
is the name of the database table to copy.
Partition number partition_number does not exist in table table_name.
partition_id
is the identifier of the partition into which to copy.
in | out
is the direction of the copy. in indicates a copy from a file into the database table; out indicates a copy to a file from the database table or view.
datafile
is the full path name of an operating system file. The path name can be from 1 to 255 characters in length.
-c
performs the copy operation with char datatype as the default. This parameter 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
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.
You cannot use the -E and -g flags together.
-n
performs the copy operation using native (operating system) formats. Specifying the -n parameter means bcp will not prompt for each field. Files in native data format are not human-readable.
Warning! Do not use bcp in native format for data recovery or 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. Do not use field terminators (-t) or row terminators (-r) with bcp in native format. Results are unpredictable and data could become corrupted. 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 re-run bcp in character format (for example, a table was truncated or dropped, hardware damage occurred, a database was dropped, and so on) the data will be unrecoverable.
-X
specifies that, in this connection to the server, the application initiates 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.
-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. Use -a in conjunction with -J to specify 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.
The following error message will appear if the character translation file(s) named with the -a parameter is missing, or you mistype the name(s):
Error in attempting to determine the size of a pair of translation tables.:'stat' utility failed.
-A size
specifies the network packet size to use for this bcp session. For example:
bcp pubs2..titles out table_out -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 you are bulk copying in; it has no effect on bulk copying out.
-e errfile
is 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 parameter.
-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 you are copying data so that you do not have to duplicate your previous format responses interactively. Use the -f parameter only if you previously created a format file that you want to use now for a copy in or copy out. If you do not specify this parameter, bcp queries you for format information interactively.
-F firstrow
is the number of the first row to copy from an input file (default is the first row).
-g id_start_value
specifies the value of the IDENTITY column to use as a starting point for copying data in.
You cannot use the -g and -E flags together.
-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. 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. See the System Administration Guide for more information about character sets and associated flags.
The following error message will appear if an incorrect or unrecognized character set is named with the -J parameter:
Unrecognized localization object. Using default value 'iso_1'.
Starting copy...
=> warning.
-L lastrow
is the number of the last row to copy from an input file (default is the last row).
-m maxerrors
is the maximum number of errors permitted before bcp aborts the copy. bcp discards each row that it cannot build, counting each rejected row as one error. If you do not include this parameter, bcp uses a default value of 10.
-P password
specifies an Adaptive Server password. If you do not specify -P password, bcp prompts for a password. You can leave out the -P flag If your password is NULL.
-q datafile_charset
runs 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 following error message will appear if the character translation file(s) named with the -q parameter is missing, or you mistype the name(s):
Error in attempting to determine the size of a pair of translation tables.:'stat' utility failed.
The ascii_7 character set is compatible with all character sets. If either the Adaptive Server character set 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 row terminator.
Warning! Do not use -t or -r parameters with bcp in native format. Results are unpredictable and data could become corrupted.
When specifying terminators from the command line with the -t or -r parameter, you must escape characters that have special significance to the Windows NT Command Prompt shell (see the following examples). Either place a backslash in front of the special character or enclose it in quotes. This is not necessary when bcp prompts you (interactive mode).
-R remote_server_principal
specifies the principal name for the server as defined to the security mechanism. By default, a server's principal name matches the server's network name (which is specified with the -S parameter or the DSQUERY environment variable). Use the -R parameter when the server's principal name and network name are not the same.
-S server
specifies the name of the Adaptive Server to which to connect. If you specify -S with no argument, bcp uses the server specified by the DSQUERY environment variable.
-t field_terminator
specifies the default field terminator.
Warning! Do not use -t or -r parameters with bcp in native format. Results are unpredictable and data could become corrupted.
-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 an 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.
-z language
is the official name of an alternate language the server uses to display bcp prompts and messages. Without the -z flag, bcp uses the server's default language. You can add languages to an Adaptive Server during installation or afterward, using the langinst utility or the sp_addlanguage stored procedure.
The following error message will appear if an incorrect or unrecognized language is named with the -z parameter:
Unrecognized localization object. Using default value 'us_english'.
Starting copy...
=> warning.
-v
reports the current version and copyright message of the bcp program.
Examples
Example 1
bcp pubs2..publishers out pub_out -c -t , -r \r
Copies data out of the publishers table in character format (using char for all fields) using the -c parameter. The -t field_terminator parameter ends each field with a comma, and the -r row_terminator parameter ends each line with a Return. bcp prompts only for a password.
Example 2
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]:
Copies data from the publishers table to a file named pub_out for later reloading into Adaptive Server. Press Return to accept the defaults specified by the prompts. The same prompts appear when you copy data into the publishers table.
Example 3
bcp pubs2..publishers out pub_out -c -t , -r \r
Copies data out of the publishers table in character format (using char for all fields). The -t field_terminator parameter ends each field with a comma, and the -r row_terminator parameter ends each line with a Return. bcp prompts only for a password.
Example 4
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]:
Copies data from the publishers table to a file named pub_out for later reloading into Adaptive Server. Press Return to accept the defaults specified by the prompts. The same prompts appear when you copy data into the publishers table.
Usage
稴ee Chapter 2, "Using bcp to Transfer Data to and from Adaptive Server" for an in-depth discussion of bcp.
稴ee the Performance and Tuning Guide for more information on how changing certain parameters can affect bcp for large batches.
穊cp provides a convenient, high-speed method for transferring data between a database table or view and an operating system file. bcp can read or write 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.
穀ou cannot copy into a view from an operating system file.
稶pon 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.
稵he current version of bcp ignores the -y sybase_directory parameter.
穀ou cannot use named pipes to copy files in or out.
稥rror 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 re-write 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.
稤o not use -t or -r parameters with bcp in native format. Results are unpredictable and data could become corrupted.
Copying Tables with Indexes or Triggers
稵he 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", to true. If the option is not set to 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 to copy data out to a file or to copy data into a table that contains indexes or triggers.
Because bcp logs inserts into a table that has indexes or triggers when you use slow bcp, 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.
稺hile the select into/bulkcopy/pllsort option is on, you cannot 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/pllsort 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.
稦ast bcp runs more slowly while a dump database is taking place.
稵able 4-2 shows which version bcp uses when copying in, the necessary settings for the select into/bulkcopy/pllsort option, and whether the transaction log is kept and can be dumped.
?
Table 4-2: Comparing fast and slow bcp
稡y default, the select into/bulkcopy/pllsort 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; re-create the indexes and triggers; and then dump the database. Remember 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) parameters, bcp prompts you only for your password, unless you supplied it with the -P parameter. If you do not supply either the -n, -c or -f formatfile parameter, bcp prompts you for information for each field in the table or view.
稥ach prompt displays a default value, in brackets, which you can accept by pressing Return. The prompts include:
稵he file storage type, which can be character or any valid Adaptive Server datatype
稵he prefix length, which is an integer indicating the length in bytes of the following data
稵he storage length of the data in the file for non-NULL fields
稵he field terminator, which can be any character string
稴cale and precision for numeric and decimal data types
The row terminator is the field terminator of the last field in the table, view, or file.
稵he 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:
稶se the default prompts
稢opy all data in the datatypes defined by their table
稶se prefixes as indicated
稤o not use terminators
稟ccept the default lengths
Table 4-3 shows the bcp prompts, defaults, and the possible alternate user responses:
Table 4-3: bcp prompts?their defaults and user responses
穊cp 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 or the Adaptive Server Reference Manual.
Be careful when you copy data from different operating systems because not all operating systems use the same native datatypes (for example, copying from NT into a UNIX server).
稟 prefix length is a 1-, 2-, or 4-byte integer that represents the length of each data value. It immediately precedes the data value in the host file.
稦ields 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(.
If data in the varchar and varbinary fields is longer than the length specified for copy out, bcp silently truncates the data in the file at the specified length.
稟 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 used tab terminators with a string that contained a tab, bcp would not be able to identify which tab represents the end of the string. bcp always looks for the first possible terminator, so, in this example it would find the wrong one.
When a terminator or prefix is present, it affects the length of data transferred. If the length of an entry being copied out to a file is less than the storage length, it is immediately followed 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 bcp is 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 the 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.
稵able 4-4 and Table 4-5 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. An ellipsis (...) indicates that the pattern repeats for each field. The field length is 8 bytes for each column; "string" represents the 6-character field each time.
?
Table 4-4: Adaptive Server char data
?
Table 4-5: Other datatypes converted to char storage
稵he 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 the types and formats copied in are incompatible with the structure of the database table, the copy fails.
稦ile storage length generally indicates the maximum amount of data that can be transferred for the column, excluding terminators and/or prefixes.
稺hen 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.
穊cp considers any data column that can contain a null value to be variable length, so use either a length prefix or a terminator to denote the length of each row of data.
稤ata 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.
稡efore copying data that is in character format from a file into a database table, check the datatype entry rules in 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 format (yy)yymmdd may result in overflow errors if the year is not specified first.
稺hen 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.
[]
- ››Office 2010 新特性:Outlook 2010
- ››Office 2010 新特性:PowerPoint 2010
- ››Office 2010 新特性: Excle 2010
- ››Office中Word十技巧 受益终身
- ››Office 2010宝典 word之利用文档导航窗格控制文档...
- ››Office 2010宝典 word之(2)利用文档导航窗格搜索文...
- ››Office 2010宝典 word之(3)可以朗读的屏幕取词翻译...
- ››Office 2010宝典 word之(4)多语言翻译
- ››Office 2010宝典 word之(5)您手边的工具:浮动工具...
- ››Office 2010宝典 word之(6)快速插入文档封面
- ››Office 2010宝典 word之(7)通过主题快速转换文档整...
- ››Office 2010宝典 word之(8)让艺术字为您的文档增色...
更多精彩
赞助商链接