Data Management

How do I use BCP in SQL Server?


The Bulk Copy Program (BCP) is a command-line utility that ships with Microsoft SQL Server. With BCP, you can import and export large amounts of data in and out of SQL Server databases quickly and easily. Any DBA who has utilized this functionality will agree that BCP is an essential tool.

BCP 101

You access the BCP utility from the command prompt. Here's the simple syntax:

bcp {dbtable | query} {in | out | queryout | format} datafile

[-n native type] [-c character type] [-S server name] [-U username]

[-P password] [-T trusted connection]

The command example I'm going to use starts with bcp followed by a fully qualified table name (database name, table or object owner, table or object name). For example, if you want to export the authors table, as part of the dbo group from the pubs database, you supply the full table name pubs.dbo.authors. Next, you use an in or out argument to specify whether you want BCP to copy data into or out of a database. You then specify the location of the datafile on your database server.

At this point, our BCP statement looks like this:

bcp pubs.dbo.authors out c: empauthors.bcp

When using BCP, don't forget that the switches are case sensitive. If you don't apply the right case, the BCP statement will fail.

Next, we'll add some of the basic command-line options. The –n switch specifies native SQL Server format. The –c switch is used when formatting the file using Char as a datatype. If you use this switch, you can easily open your file with Excel. The –S switch enables you to add the server/instance name. The –U switch allows you to add the name of the login used to connect to SQL Server. The –P switch lets you add the password of the –U switch, and the –T switch is for establishing a trusted connection to your SQL Server. Figure A shows our completed BCP statement.

If we were importing data to the authors table, our BCP command would look like this:

bcp pubs.dbo.authors in c: empauthors.bcp -c -Sstevenw -Usa –P That gives you an idea of the basics of the BCP utility, but there are plenty more switches at your disposal. In all, BCP supports 27 switches, which are shown in Listing A.

With this many switches available, I recommend that you play around with them to see which ones you might want to use. For example, the –e switch is handy because it will create an error file you can look at if your BCP command returns errors. Here is an example:

bcp pubs.dbo.authors out c: empsteventest.txt -n -Sstevenw -Usa -P -eC: emperror.txt

For more information on how to use the various switches, see SQL Server Books Online.

BCP and database objects

Let's create a new database called pubs2 and use BCP to move all the data from the pubs database into it. First, we need to create the database. Then, we'll generate the SQL script, run it on pubs2, and export the data from pubs into pubs2.

Start by creating the database in SQL Server Enterprise Manager and name it pubs2. Next, select pubs, right-click on it, and choose Generate SQL Scripts from the All Tasks menu. Now you are ready to set your scripting options. Click Show All and then select the Script All Objects check box, as shown in Figure B . Go to the Options tab and select all the check boxes under Table Scripting Options, as we've done in Figure C . You can preview your script from the General tab or click OK to create and save the script.

By performing the above, we are making a complete replica of the Pubs database. We could also have scripted out only one table or multiple tables. You can make the choice, depending on the task at hand.

Now, open Query Analyzer and run the newly created script (Figure D ). After the script runs, the database objects and indexes will have been successfully created (Figure E ).

To export the authors table out of pubs and import it into pubs2, we'll use the BCP statements we learned earlier:

bcp pubs.dbo.authors out c: emppubauthors.bcp –n –Sstevenw –Usa –P bcp pubs2.dbo.authors in c: emppubauthors.bcp –n –Sstevenw –Usa –P

Then, open the SQL Query Analyzer and run the following statements on the pubs2 and pubs databases:

Use pubs2

Select * from authors

Select count(*) from authors

Use pubs

Select * from authors

Select count(*) from authors

This allows you to see the full process of moving data come full circle.

Most DBA's today use DTS, Database Restore, and/or attaching and detaching databases to copy data from one server to another. All these tools will get the job done. But if you want to copy large amounts of SQL Server data quickly, BCP is still one of the best tools available. Once you get past the initial learning curve, you'll find that BCP offers a highly efficient way to copy data from one database to another.

Editor's Picks