Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

March 21, 2001 12:00 AM

Format File Overview

SQL Server Pro
InstantDoc ID #19762
Rating: (0)

The first two lines of the format file contain the version number of the bulk copy program (bcp) that the format file was created to use (8.0 for SQL Server 2000's bcp) and the number of data columns in the data file, respectively. Each remaining line defines the correspondence between one data field in the file and one column in the SQL Server table. Each of these lines must contain the following eight fields of information:

  1. The number of the field in the data file (starting with 1).
  2. The format of the field as stored in the data file (e.g., SQLCHAR for external character fields, SQLNCHAR for fields stored in Unicode character format, and SQLINT for binary integer fields in the data file). The next three fields combine to describe the length of the field in the data file and support both fixed-length and variable-length fields.
  3. The Prefix Length field, which stores the data field's actual length in bytes. The prefix field can be 0, 1, 2, or 4 bytes long, and SQL Server Books Online (BOL) describes the minimum length that you must specify for different data types.
  4. The Host File Data Length, which is the maximum number of bytes this field will occupy in the data file. The bcp operation uses this field only when the Prefix Length is zero—meaning bcp doesn't have access to the actual data length—and you haven't designated a Field Terminator for the field. You specify a fixed-field format for the data field by setting a zero-length prefix field and no Field Terminator. The bcp operation uses the number of bytes specified for the data-file field's Host File Data Length.
  5. The Field Terminator string, which is a sequence of one or more characters that don't appear in the field's data and that bcp uses to signify the end of that data field. A null string ("") instructs bcp not to use a Field Terminator string. The bcp operation defaults to using the newline character (specified as "\n") for the row's last field and the Tab character (specified as "\t") for other fields. When you specify a Field Terminator for a field, the length of the data field that bcp writes to the data file varies only when the table column is of a variable-length data type and you requested a character or Unicode character format for the data-file field. Otherwise, when the database column is of fixed length or bcp writes the field to the file in a SQL Server native data format, the field's length in the data file is the same for all data rows. When you don't use a Field Terminator string with fixed-length fields or when you ask bcp to write a field to the data file in SQL Server native data format, you must specify the correct number of bytes that the field will occupy in the data file. In any case, you can easily get the correct number of bytes by letting bcp generate the format file during an export operation. The next two fields in the format file designate the column in the SQL Server table that corresponds to the data-file field that this record describes. The bcp operation relies on the first of these two fields.
  6. The column number in the SQL Server table that bcp will copy the data to or from. You can display table column numbers by using the sp_help system stored procedure.
  7. The column's name in the SQL Server table. This field only documents the column name; bcp ignores it. You can't, however, leave this field blank.
  8. The SQL Server collation for character data in the data file. You can let bcp generate the format file with the proper collation specification for character fields; for noncharacter fields, specify a null string ( "" ).

Related Content:

ARTICLE TOOLS

Comments
  • Ben
    8 years ago
    Apr 28, 2004

    Looking for the format of BCP's native (-N) binary file. I have solved most datatypes but am stuck on floats. Basically, if you export a float value, say for example a real datatype 1.0 - how is it encoded and stored in the binary file? The value in the file for a decimal 1 is 80 3F hex.. what is that? (note - field delimiter is the number of bytes used to store the field if it allows nulls)

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.