| Lesson 5 | The bulk copy program (bcp) |
| Objective | Use the bulk copy program in SQL Server |
The bulk copy program (bcp) is a command-line utility that moves large volumes of data
between SQL Server and a flat file. You can use it to:
in)out)queryout)format)
If you already understand BULK INSERT, think of bcp as the command-line equivalent that also
supports exporting data (which BULK INSERT does not).
bcp is available on the machine that will run the command (workstation, jump box, VM, build agent).
Verify:
bcp -v
bcp
(or on a mounted share with appropriate throughput and permissions).
-T for Windows integrated authentication (common on domain-joined systems)-G for Microsoft Entra authentication when appropriate (especially for Azure-hosted services)INSERT. Export requires SELECT.
If you are loading into staging tables, ensure the login has access to the target schema.
At a high level:
bcp [database_name.]schema.{table_name | view_name | "query"}
{ in data_file | out data_file | queryout data_file | format nul }
[options]
The in/out/queryout/format keyword tells bcp what direction the copy is going.
The remaining switches control formatting, authentication, batching, and performance.
queryout to export a result set.TABLOCK) – performance/behavior hints for imports.Goal: load a CSV into Sales.dbo.Clients.
bcp Sales.dbo.Clients in "C:\data\clients.csv" ^
-S localhost ^
-T ^
-c ^
-t, ^
-r "\n" ^
-b 50000 ^
-m 10 ^
-e "C:\data\clients_import_errors.log" ^
-o "C:\data\clients_import_output.log"
Best practice: import into a staging table first, validate row counts and data quality, then move into the final table with set-based SQL in a controlled transaction.
Goal: export a table to CSV.
bcp Sales.dbo.Clients out "C:\data\clients_export.csv" ^
-S localhost ^
-T ^
-c ^
-t, ^
-r "\n"
If the output file exists, bcp overwrites it. For repeatable exports, standardize a directory layout and a
naming convention (timestamped files, environment tags, etc.).
queryout lets you export a filtered dataset or a joined result set. The query must be quoted.
If the query returns multiple result sets, only the first one is exported.
bcp "SELECT ClientID, Company, City, State, Zip
FROM Sales.dbo.Clients
WHERE State IN ('AZ','CA')
ORDER BY Company" queryout "C:\data\clients_AZ_CA.csv" ^
-S localhost ^
-T ^
-c ^
-t, ^
-r "\n"
Best practice: keep the query in source control (or generate it from a stored view) so exports remain consistent across environments.
Format files are the key to reliable, repeatable bulk operations. A bcp data file contains data,
not schema. A stored format file documents the column order, types, and delimiters used for that table.
Create a non-XML format file:
bcp Sales.dbo.Clients format nul ^
-S localhost ^
-T ^
-c ^
-f "C:\data\Clients.fmt"
Create an XML format file (Windows utility supports -x):
bcp Sales.dbo.Clients format nul ^
-S localhost ^
-T ^
-c ^
-x ^
-f "C:\data\Clients.xml"
bcp Sales.dbo.Clients in "C:\data\clients.csv" ^
-S localhost ^
-T ^
-f "C:\data\Clients.fmt" ^
-e "C:\data\clients_import_errors.log"
Best practice: store format files beside the import/export job definition (and treat them like code). If the table changes, update the format file deliberately rather than “fixing” the job ad hoc.
-b to reduce the blast radius of failures and manage log growth.-e for imports so you can inspect rejected rows.-h hints (for example TABLOCK) when appropriate for large loads,
especially into empty staging tables where locking behavior is acceptable.
-o and keep a job run history.-T or -G and managed identity patterns where feasible.bcp Sales.dbo.Employee in "C:\data\employee.tsv" ^
-S WesternRegion ^
-T ^
-c ^
-t "\t" ^
-r "\n"
employee.tsv into Sales.dbo.Employee.WesternRegion server using integrated security.In the next lesson, you will be introduced to importing data using built-in tooling and common workflows for moving data between systems.
GROUP BY with ROLLUP or
separate aggregation queries, then export a single, well-defined result set.