INF: Using BCP & .FMT File to Read in Only Specified Columns

Last reviewed: March 12, 1997
Article ID: Q50636

The information in this article applies to:

  - Microsoft SQL Server for OS/2 version 4.2
  - Microsoft SQL Server version 4.2

You can read in only specified columns by using BCP and making modifications to the format file with some restrictions. You can modify the .FMT file to enable a bulk copy of data from only specific columns. The remaining columns will be filled with NULLS and the column format must be set up to allow NULLs when the table is created.

To do this, do the following:

  1. Use BCP to create the output data file and the format file.

  2. Load the format file into a text editor.

For example, if your table consists of four tables of simple integer data with column names of a, b, c, and d, you will see a format file that looks similar to the following (if you are using SQL Server version 4.2, substitute 4.2 for 4.0 in the first line of this example):

    4.0
    4
    1          SYBINT4 0       4       ""     1      a
    2          SYBINT4 0       4       ""     2      b
    3          SYBINT4 0       4       ""     3      c
    4          SYBINT4 0       4       ""     4      d

To modify this file to read in only the first and last columns of data, you need to change the file to the following (if you are using Microsoft SQL Server version 4.2, substitute 4.2 for 4.0 in the first line of this example):

    4.0
    2
    1          SYBINT4 0       4       ""     1      a
    2          SYBINT4 0       4       ""     4      d

The important changes are made on the second line, which specifies the number of descriptors to follow, and the column specifiers, which are the numbers 1 and 4 in the next-to-last column. The actual names of the columns(a,d) are not important to the process.

Once the .FMT file has been modified and saved, perform a BCP call that reads back into the database (or a different or newly created database), specifying this format file with the -f option.


Additional query words: 4.20 Windows NT
Keywords : SSrvBCP SSrvWinNT
Version : 4.20
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 12, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.