Enterprise Business Rules · Module 7

Understand the bcp Bulk Copy Utility

Lesson 4

Understand the bcp Bulk Copy Utility

The bcp utility runs from an operating-system shell. It transfers rows between SQL Server and a data file. It is not a Transact-SQL statement and should not be pasted into an SSMS query window.

Read the command structure

bcp database.schema.table out "C:\data\rows.tsv" -S SERVER -T -c

Here out writes table data to a file, -S selects the server, -T uses Windows integrated authentication, and -c uses character format. These examples assume a Windows workstation with bcp installed and an authorized Windows login. Check the installed client with bcp -v; client capabilities depend on the utility version as well as the database server.

DirectionPurpose
inLoad a file into an existing target table
outWrite table or view rows to a file
queryoutWrite the result of a supplied query

Agree on the file format

Character files are useful for interchange. Native formats are intended for compatible SQL Server transfers. The data file does not carry a complete table definition, so keep the schema and format contract with it. A format file can describe mappings when file and table layouts differ.

A comma terminator alone does not make bcp a general CSV serializer with automatic quoting of embedded commas and newlines. Use a suitable CSV-capable workflow when the data requires quoting and escaping. For simple tab-separated demonstrations, prohibit tabs and line breaks inside values.

Know where a path is resolved

With bcp, the file is accessed by the machine running the utility. With BULK INSERT, file access is performed from the SQL Server side under the applicable security context. A file on your workstation is not automatically a file on the database server.

Operational checks

Review the process exit status and transfer messages, then query the destination to validate the business result. Plan permissions, constraints, rejected rows, and retry behavior. Avoid embedding passwords in command files, and do not enable xp_cmdshell just to run the examples.


Microsoft references