Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

Using the bcp options [Table of Contents] Using format files

Utility Guide

[-] Chapter 3 Using bcp to Transfer Data to and from Adaptive Server
[-] Changing the defaults: interactive bcp

Changing the defaults: interactive bcp

If you do not specify native (-n) or character (-c) format, bcp prompts you interactively for:

The default values for these prompts produce the same results as using the native format and provide a simple means for copying data out of a database for later reloading into Adaptive Server.

If you are copying data to or from Adaptive Server for use with other programs, base your answers to the prompts on the format required by the other software.

These four prompts provide an extremely flexible system that allows you either to read a file from other software or to create a file that requires little or no editing to conform to many other data formats.

The following sections discuss these prompts and the way they interact to affect 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.

File storage type

The file storage type prompt offers you choices about how to store the data in the file. You can copy data into a file as:

Table 3-4 shows the default storage type for each Adaptive Server datatype and the abbreviations that are acceptable to bcp.

In Table 3-4, brackets [ ] indicate that you can use the initial character or the beginning characters of the word. For example, for "bit" you can use "b," "bi," or "bit."

File storage datatypes for bcp

Table datatype

Storage type

char, varchar

c[har]

text

T[ext]

int

i[nt]

smallint

s[mallint]

tinyint

t[inyint]

float

f[loat]

money

m[oney]

bit

b[it]

datetime

d[atetime]

binary, varbinary, timestamp

x

image

I[mage]

smalldatetime

D

real

r

smallmoney

M

numeric

n

decimal

e

To display this list while using bcp interactively, type a question mark (?) in response to the prompt "Enter the file storage type".

The suggested values that appear in the prompts are the defaults. Remember that your response determines how the data is stored in the output file; you need not indicate the column's type in the database table.

bcp fails if you enter a type that is not either implicitly convertible or char. For example, you may not be able to use smallint for int data (you may get overflow errors), but you can use int for smallint.

When storing noncharacter datatypes as their database types, bcp writes the data to the file in Adaptive Server's internal data representation format for the host operating system, rather than in human-readable form.

Before copying data that is in character format from a file into a database table, check the datatype entry rules in the Sybase Adaptive Server Enterprise Reference Manual. Character data 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.

Prefix length

By default, bcp precedes each field that has a variable storage length with a string of one or more bytes indicating the length of the field. This prefix enables the most compact file storage.

The default values in the prompts indicate the most efficient prefix length:

A prefix length is a 1-, 2-, or 4-byte integer that represents the length of each data value in bytes. It immediately precedes the data value in the host file.

Unless you supply a terminator, bcp pads each stored field with spaces to the full length specified at the next prompt, "length."

Because prefix lengths consist of native format integers, the resulting host file contains nonprintable characters. The nature of these characters could prevent you from printing the host file or from transmitting it through a communications program that cannot handle non-human-readable characters.

For more information about prefix lengths, see Table 3-9.

Field length

In almost all cases, use the bcp default value for the storage length while copying data out.

The terms "length" and "storage length" in this section refer to the operating system file, not to Adaptive Server field lengths.

Because you can change the default length by supplying another value, you must be familiar with the data to transfer. If you are copying character data in from other software, examine the source file carefully before choosing length values.

If the storage type is noncharacter, bcp stores the data in the operating system's native data representation and does not prompt for a length.

When bcp converts noncharacter data to character storage, it suggests a default field length that is large enough to store the data without truncating datetime data or causing an overflow of numeric data.

Field and row terminators

You can use a terminator to mark the end of a column or row, separating one from the next. The default is no terminator.

Terminators are very useful for dealing with character data because you can choose human-readable terminators. The bcp character option, which uses tabs between each column with a newline terminator at the end of each row, is an example of using terminators that enhance the readability of a data file.

When you prepare data for use with other programs, and when you want to use bcp to prepare tabular data, supply your own terminators. The available terminators are:

Control characters (ASCII 0-25) cannot be printed.

Choosing Terminators

Choose terminators with patterns that do not appear in any of the data.

For example, using a tab terminator with a string of data that also contains a tab creates an ambiguity: which tab represents the end of the string? bcp always looks for the first possible terminator, which in this case would be incorrect, since the first tab it would encounter would be the one that is part of the data string.

Data in native format can also conflict with terminators. Given a column that contains a 4-byte integer in native format, if the values of these integers are not strictly limited, it will be impossible to choose a terminator that is guaranteed not to appear inside the data. Use bcp's native format option for data in native format.

"No terminator" is different from a "null terminator," which is an invisible, but real, character.


Using the bcp options [Table of Contents] Using format files