How to Export a SQL Server Table to CSV

Galaxy Glossary

How do I export a SQL Server table to CSV with bcp?

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.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why export a table to CSV?

CSV is portable, human-readable, and supported by Excel, BI tools, and other databases. Exporting makes ad-hoc analysis, data exchange, and backups simple.

What are the main export options?

Use bcp (command-line), sqlcmd with -o, SSMS Export Wizard, or PowerShell. bcp is fastest and script-friendly, so this guide focuses on it.

How do I export a single table?

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.

Can I export filtered rows?

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.

What about headers?

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.

How do I schedule exports?

Create a SQL Agent job or Windows Task Scheduler entry that runs the .bat or PowerShell script containing your bcp command.

Best practices

  • Write to a staging folder with write permissions.
  • Quote paths with spaces.
  • Use trusted auth (-T) or securely store credentials (-U/-P).
  • Validate file size and record count post-export.

.

Why How to Export a SQL Server Table to CSV is important

How to Export a SQL Server Table to CSV Example Usage


REM Export all paid Orders in 2024 to CSV
bcp "SELECT o.id,o.order_date,o.total_amount,c.name,c.email FROM dbo.Orders o JOIN dbo.Customers c ON c.id=o.customer_id WHERE YEAR(o.order_date)=2024" queryout "C:\exports\orders_2024.csv" -c -t , -S localhost -d EcommerceDB -T

How to Export a SQL Server Table to CSV Syntax


bcp {[[database_name].schema].table_name | "query"} {out | queryout} data_file
  [-S server_name[\instance]]      -- SQL Server instance
  [-d database_name]               -- Target database
  [-T | -U username -P password]   -- Authentication
  [-c]                             -- Character data, no format file
  [-t field_term]                  -- Field delimiter (comma)
  [-r row_term]                    -- Row delimiter (\n)
  [-C {ACP | OEM | RAW | code_page}]-- Code page
Example (full table):
bcp dbo.Customers out "C:\exports\customers.csv" -c -t , -S localhost -d EcommerceDB -T
Example (query with join):
bcp "SELECT c.id,c.name,p.name,p.price FROM dbo.Customers c JOIN dbo.Orders o ON o.customer_id=c.id JOIN dbo.OrderItems oi ON oi.order_id=o.id JOIN dbo.Products p ON p.id=oi.product_id" queryout "C:\exports\customer_products.csv" -c -t , -S localhost -d EcommerceDB -T

Common Mistakes

Frequently Asked Questions (FAQs)

Can I include column headers?

Add them manually or post-process the file. bcp itself exports only data rows.

How large a table can I export?

bcp streams rows and handles multi-GB tables. Ensure disk space and consider -a packet size for very wide rows.

Is bcp available on Linux?

Yes. Install the Microsoft ODBC driver and mssql-tools package, then run the same commands.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.