Administering SQL Server

Preparing Data for Importing and Exporting

In order for the bcp and BULK INSERT utilities to insert data, the data file must be in row and column format. Microsoft® SQL Server™ can accept data in any ASCII or binary format as long as the terminators (characters used to separate columns and rows) can be described. The structure of the data file does not need to be identical to the structure of the SQL Server table because bcp and BULK INSERT allow columns to be skipped or reordered during the bulk copy process.

Data that is bulk copied into an instance of SQL Server is appended to any existing contents in a table. Data that is bulk copied from an instance of SQL Server to a data file overwrites the previous contents of the data file.

To bulk copy data:

Before using bulk copy operations, consider the following:

Importing and Exporting Data Example

To bulk copy data from the publishers table in the pubs database to the Publishers.txt data file in ASCII text format, from the command prompt, execute:

bcp pubs..publishers out publishers.txt -c -Sservername -Usa -Ppassword

The contents of the Publishers.txt file:

0736 New Moon Books Boston MA USA
0877 Binnet & Hardley Washington DC USA
1389 Algodata Infosystems Berkeley CA USA
1622 Five Lakes Publishing Chicago IL USA
1756 Ramona Publishers Dallas TX USA
9901 GGG&G München -- Germany
9952 Scootney Books New York NY USA
9999 Lucerne Publishing Paris -- France

Conversely, to bulk copy data from the Publishers.txt file into the publishers2 table in the pubs database, from the command prompt, execute:

bcp pubs..publishers2 in publishers.txt -c -Sservername -Usa -Ppassword

Alternatively, you can use the BULK INSERT statement from a query tool, such as SQL Query Analyzer, to bulk copy data:

BULK INSERT pubs..publishers2 FROM 'c:\publishers.txt' 
WITH (DATAFILETYPE = 'char')

Note  The publishers2 table must be created first.

See Also

bcp Utility

BULK INSERT.

Character Format

ExportData Method

ImportData Method

Managing Security Accounts

Native Format