Exporting a SQL Server table to CSV writes row data to a delimited text file that can be opened in spreadsheets or loaded into other systems.
CSV is portable, human-readable, and supported by Excel, BI tools, and other databases. Exporting makes ad-hoc analysis, data exchange, and backups simple.
Use bcp
(command-line), sqlcmd
with -o
, SSMS Export Wizard, or PowerShell. bcp
is fastest and script-friendly, so this guide focuses on it.
Run bcp dbo.Customers out C:\exports\customers.csv -c -t , -S localhost -d EcommerceDB -T
.The switch -c
outputs char data, -t ,
sets the comma delimiter, and -T
enables trusted Windows auth.
Supply a queryout
: bcp "SELECT * FROM dbo.Orders WHERE order_date >= '2024-01-01'" queryout C:\exports\recent_orders.csv -c -t , -S localhost -d EcommerceDB -T
.
bcp
does not add headers.Prefix them by piping PowerShell: "id,name,email" | Out-File -Encoding ascii C:\exports\customers.csv; bcp "SELECT id,name,email FROM dbo.Customers" queryout C:\exports\customers.csv -c -t , -S localhost -d EcommerceDB -T -a 32768 -C ACP -Append
.
Create a SQL Agent job or Windows Task Scheduler entry that runs the .bat
or PowerShell script containing your bcp
command.
-T
) or securely store credentials (-U
/-P
)..
Add them manually or post-process the file. bcp itself exports only data rows.
bcp streams rows and handles multi-GB tables. Ensure disk space and consider -a
packet size for very wide rows.
Yes. Install the Microsoft ODBC driver and mssql-tools package, then run the same commands.