Copying Data from a Data File to SQL Server

To bulk copy a data file to Microsoft® SQL Server™, follow these guidelines:

To bulk copy data successfully into a table from a data file with the bcp utility or BULK INSERT statement, the terminators in the data file must be known, and specified.

The Newpubs.dat file:

1111,Stone Age Books,Boston,MA,USA

2222,Harley & Davidson,Washington,DC,USA

3333,Infodata Algosystems,Berkeley,CA,USA

  

Because the data file is all character data, the following options and parameters need to be specified.

Bulk copy option bcp utility parameter BULK INSERT clause
Character mode format -c DATAFILETYPE = 'char'
Field terminator -t FIELDTERMINATOR
Row terminator -r ROWTERMINATOR

In the Newpubs.dat file, each field in a row ends with a comma (,); each row ends with a newline character (\n).

The publishers2 table in the following example can be created by executing:

USE pubs

GO

sp_dboption pubs, 'select into/bulkcopy', 'true'

GO

SELECT * INTO publishers2 FROM publishers

GO

  

The command to bulk copy data from Newpubs.dat into publishers2 is:

bcp pubs..publishers2 in newpubs.dat -c -t , -r \n -Sservername -Usa -Ppassword

  

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

BULK INSERT pubs..publishers2 FROM 'c:\newpubs.dat'
WITH (

    DATAFILETYPE = 'char',
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n'

)

  

Data from the Newpubs.dat file has been now appended to publishers2:

pub_id pub_name city state country
------ ---------------- ----------   -----
0736 New Moon Books Boston MA USA
0877 Binnet & Hardley Washington DC USA
1111 Stone Age Books Boston MA USA
1389 Algodata Infosystems Berkeley CA USA
1622 Five Lakes Publishing Chicago IL USA
1756 Ramona Publishers Dallas TX USA
2222 Harley & Davidson Washington DC USA
3333 Infodata Algosystems Berkeley CA USA
9901 GGG&G München   Germany
9952 Scootney Books New York NY USA
9999 Lucerne Publishing Paris   France

See Also
ImportData Method SuspendIndexing Property
UseBulkCopyOption Property Creating and Restoring a Database Backup

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.