SQL Server 2016: Save Query Results to a CSV File
You can save the results of any query to a CSV file with little more than a click of the mouse.
SQL Server makes it easy for you to save the results of a query to a CSV file (or other text file).
Here's how.
-
Query Results
-
Save the File
-
Open the File
Add a Header Row
You might have noticed that the above CSV file doesn't contain a header row.
This may or may not be what you want.
If you need it to contain a header row, do the following.
-
Launch the Query Options Dialog Box
-
Include Column Headers
-
Save another Query and Check the File
Results to File
You can also use the Results to File option for saving your query results to a file.
This option will prompt you to save the results whenever you run a query. So this can be handy if you have many queries to save.
-
Select the Results to File Option
-
Run a Query
-
View the File
Results to File Options
You might have noticed that the above screenshot shows that the text file displays the results in column aligned format (not comma delimited like in the previous file). This can be changed if required.
You can use the Query Options dialog box to change various options for when using the Results to File method.
-
Launch the Query Options Dialog Box
-
Configure the Desired Options
Results to Text
You can also use Results to Text for outputting the results.
This can be handy when configuring the query formatting options for Results to File (as it uses the same query options). This will enable you to check the formatting without having to open an external file.
To switch to Results to Text, click the Results to Text icon in the toolbar. You can toggle between output options whenever you need.