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:
The number of fields in the data file does not have to match the number of columns in the table or be in the same order.
Each column in the table must be compatible with the field in the data file being copied. For example, it is not possible to copy an int field to a datetime column using native format bcp.
To bulk copy data from a data file into a table, you must have INSERT and SELECT permissions on the table. To bulk copy a table or view to a data file, you must have SELECT permission on the table or view being bulkcopied.
Before using bulk copy operations, consider the following:
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.