Export SQL Server Query Results to a CSV File
Question details
Save the results of a SQL Server SELECT query as 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.
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.
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.
In SSMS, open Tools > Options > Query Results > SQL Server > Results to Text. Set Output format to Comma delimited and apply the change.
Open a new query window after changing the output settings, then enter or paste your SELECT query.
Select Query > Results To > Results to File, or press Ctrl+Shift+F.
Click Execute. In the save dialog, enter a file name ending in .csv, choose a location, and save the output.

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. Run the query in SSMS: In SSMS, select Query > Results To > Results to Grid, then click Execute to run your SELECT query.
- 2. Copy the result grid: Select the result grid using its upper-left corner, right-click the selection, and choose Copy with Headers.
- 3. Paste into WPS: Open a blank workbook in WPS Spreadsheets, select cell A1, and paste the copied results.
- 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.

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.




