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



The information in this article applies to:

  • Microsoft SQL Server 4.2x

This article was previously published under Q50636
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.

Modification Type:MinorLast Reviewed:2/14/2005
Keywords:KB50636