Prashanth Jayaram

The BCP (Bulk Copy Program) command in action

August 13, 2018 by

There are various methods available for bulk data operations.

  1. BCP utility
  2. BULK INSERT
  3. Using OPENROWSET
  4. Import/Export wizard

The BCP (Bulk Copy Program) utility is a command line that program that bulk-copies data between a SQL instance and a data file using a special format file. The BCP utility can be used to import large numbers of rows into SQL Server or export SQL Server data into files. The BCP data files don’t include any schema details or format information. Hence, it is recommended to create a format file to record the data format so in case of any failures, you can refer to the format file and better understand the data format to determine what may have gone wrong..

We’ve been using the BCP tool for a long time, the reason being that it has a very low overhead, and works great for bulk exporting and importing of data. It is one of the most efficient ways to handle bulk import and export of data.

Article overview

In this article, the BCP utility will be explained in detail. It covers the following topics:

  1. BCP import and export command
  2. The format file
  3. How to create format file
  4. How to use the format file for data management
  5. and more…

Configuration

SQL Server supports exporting of data from a SQL Server table and importing the same into another table. To run the BCP command in ssms, enable the xp_cmdshell server configuration parameter. This gives you the control to run extended stored procedures. Keep in mind, though, that this is not always a recommended option, since it exposes the SQL Server surface to potential threats from the outside world.


BCP IN | OUT | QUERYOUT options

OUT: This option is used to export (or dump) all the records from a table into a data file. For example,

  1. SQLShackDemoATC.dbo.SQLShackDemo – This is the name of the table that we intend to export. We can also use -d option to include the database name.
  2. c :\SODetail_Out.txt – This is the output file where the data is dumped to
  3. -T – Trusted Windows authentication
  4. -t, – define comma as the field separator
  5. -w – Use wide width data format
  6. -b1000 – Export the data in batches of 1000 rows

IN: This option is used to import all the records to an existing table. This requires the table to be created before executing the BCP command.

Let’s create a table to import the data, called SalesOrderDetailsIn using the following create statement

We can see that in the following BCP command that the IN keyword is used to import the data from the SODetails_Output.txt file.

BCP QUERYOUT

In the following example, we’re not going to export an entire table. Let’s look at a way to run a SQL statement to generate a data dump. The BCP export command is used in conjunction with the SELECT statement and the queryout option. Using queryout the SQL statement can be run on a defined connection and the data can be dumped into another file; the other switches are the same in this example as well.

Format files

The BCP utility supports the use of a format file that contains the formatting details of each field in a file. The format file is used to provide all the required formatting details for bulk export and bulk import operations.

  1. It provides a flexible way to interpret the data.
  2. It provides an interface to re-format the data during the export process.
  3. The format file eliminates the need of special programs for data import and export operations.

SQL Server supports two kinds of format files: XML format files and non-XML format files. The non-XML format file is the original format supported by earlier versions of SQL Server.

In this section we shall walk through some of the concepts of the format file, look at how to create the format file, and other details.

Let’s Jump in and get started:

Creating a Non-XML Format File

A non-XML format file is an ordinary file with a well-defined structure, and contains metadata about the column such as storage type, prefix length, field length, and field terminator.

The following BCP command is used to create format file:

  1. Version 10.0 is the version number of the BCP utility.
  2. Number of columns is the number of fields in the data file; in this case, it’s 3.
  3. The other fields in the format file describe the nature of the data fields.
  4. Field Order indicates the position of each field.
  5. Data Type describers the type of the data.
  6. Prefix Len is the length for the prefix characters.
  7. Data Len is the maximum allowed length for the data for the specific field.
  8. Terminator is the delimiter used to separate any two fields of the data.
  9. Column Order is the SQL Server table column order.
  10. Column Name is the name of the SQL Server table column.
  11. Column Collation is the collation used to store the characters in the data file.

If -f is used with the format option, the specified format file is created for the specified table or view. To create an XML format file, specify the -x option.

Let’s see an example to skip ENAME column of a table and load the data into the SQLShackDemoSkip table using a format file. Before changing the format file, let’s create the SQLShackDemoSkip table and a data file with EID and EJOB data using the SQLShackDemo table.

Run the BCP command to generate the data file:

Following is the output of the data file:

Now, modify the format file, the first data field maps to EID, skips ENAME, and the third row maps the second data field of the data file to EJOB. The loading should ignore the ENAME column.

To skip a table column, modify the format file definition of the corresponding row and set the values to 0 for all the related columns as shown below.

Run the following BCP command to load the data into the SQLShackDemoSkip table.


The loading process excludes the second column of SQlShackDemoSkip.

Summary

The BCP Utility, that’s very familiar to all SQL Server administrators, operates really well and is fast as well as efficient in terms of data import and export. Most of the options in the BCP command are case sensitive. It is recommended that we assume everything as case sensitive, so that we never run into troubles using the command.

BCP is capable of exporting and importing really large chunks of data, but if you’re exporting/importing data in the range of tens of millions, it’s recommended to break it into smaller chunks. And this can be using the SELECT statement with the WHERE clause. When we’re bringing the data in, we need to be cautious of the defined constraints. Sometimes we need to delete the constraint and dump the data, and then re-add those deleted constraints.


Table of contents

Getting started building applications using SQL Server DevOps Tools
Overview of SQLCMD utility in SQL Server
The BCP (Bulk Copy Program) command in action
Continuous Database Delivery (CD) using SQL Server Tools SqlPackage.exe
All about MSSQL-Scripter, the SQL Server cross-platform scripting Tool
Getting started with SQL Operations Studio (SOS); initial installation and configuration

Prashanth Jayaram
168 Views