Others

Export SQL Server Query Results to a CSV File

Algirdas JasaitisAlgirdas Jasaitis Nov 22, 2026 868 views

Question details

Save the results of a SQL Server SELECT query as a CSV file.

How to Export SQL Server Query Results to a CSV File
Product
SQL Server Management Studio
Device & OS
not provided
Scenario
The user wants to export query results to a file from a SQL Server query window.
Observed behavior
Oracle-style SPOOL commands and SQLCMD output syntax cause syntax errors when entered in the standard query window.
Before you start

Open SQL Server Management Studio (SSMS), connect to the relevant server, and prepare the SELECT query you want to export. Check whether your results contain commas, quotation marks, or line breaks, as these can affect CSV formatting.

Solution 1Recommended

Save SQL Server Query Results to a File in SSMS

Use SSMS Results to File and configure comma-delimited output before running the query.

SPOOL is an Oracle command, not a standard SQL Server query-editor command. SSMS can write query results to a file without adding an export command to the SELECT statement.

1
Set the output format

In SSMS, open Tools > Options > Query Results > SQL Server > Results to Text. Set Output format to Comma delimited and apply the change.

2
Open a query window

Open a new query window after changing the output settings, then enter or paste your SELECT query.

3
Choose Results to File

Select Query > Results To > Results to File, or press Ctrl+Shift+F.

4
Run and save the results

Click Execute. In the save dialog, enter a file name ending in .csv, choose a location, and save the output.

Save SQL Server Query Results to a File in SSMS
Check the exported CSV: Comma-delimited text output may not quote values containing commas, quotation marks, or line breaks as a CSV reader expects. Open the file and verify the columns; for complex data, use an export method that supports CSV text qualifiers.
Export query results with WPS

Save SQL Server Results as CSV with WPS Spreadsheets

Copy a query result grid from SSMS into WPS Spreadsheets, then save the sheet as a CSV file. SSMS is still needed to run the SQL Server query.

  1. 1. Run the query in SSMS: In SSMS, select Query > Results To > Results to Grid, then click Execute to run your SELECT query.
  2. 2. Copy the result grid: Select the result grid using its upper-left corner, right-click the selection, and choose Copy with Headers.
  3. 3. Paste into WPS: Open a blank workbook in WPS Spreadsheets, select cell A1, and paste the copied results.
  4. 4. Save as CSV: Choose File > Save As, select CSV as the file type, and save the file. Confirm any format prompts, then reopen the CSV to check the values.
Review SQL Server query results in a spreadsheet before saving them as CSVOpen and edit Microsoft Excel XLS and XLSX filesUse a familiar spreadsheet interface to inspect rows and columns
microsoft office alternative - wps office

Frequently Asked Questions

Why does SPOOL cause a syntax error in SQL Server?

SPOOL is an Oracle command and is not supported by the standard SSMS query editor. Use Query > Results To > Results to File instead.

Will naming the output file .csv automatically make it comma-delimited?

No. In SSMS, first set Tools > Options > Query Results > SQL Server > Results to Text > Output format to Comma delimited. Then open a new query window and save the results with a .csv extension.

How can I include column names in the exported results?

In SSMS, check the column-header option under Tools > Options > Query Results > SQL Server > Results to Text before opening a new query window. For grid output, select the results and use Copy with Headers.

Can I use WPS Spreadsheets without running SQL commands in it?

Yes. Run the query in SSMS, copy the results from the grid, and paste them into WPS Spreadsheets. Use File > Save As to save the sheet as CSV, then check that the values and columns are correct.