HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

In SQL Server, how do I save the results of a query to a txt file?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thefilesqlquerysavehowserverresultstxt

Problem

In SQL Server, how do I export the results of a query to a txt file. I tried tools>options>results to file. But this appears to have saved the Messages the query generated instead of the Results the query generated.

Solution

Options:

  • Use sqlcmd at the command line to execute your SQL statement; the results will arrive as text and can be trivially saved.



  • See https://stackoverflow.com/questions/425379/how-to-export-data-as-csv-format-from-sql-server-using-sqlcmd



  • Use bcp at the command line to execute your SQL statement with the "queryout" parameter.



  • Use SSMS to grid as you are, right-click the upper left cell of the results pane and use the Save Results As... option



  • Use SSMS to file; this will of course save the file on the machine SSMS is running on. Tools->Options->Results to file is where you set options like delimiter, maximum size, and so on. Instead, you want Query->Results To->Results to File or Ctrl-Shift-F (by default)



  • Other non-SQL ways like Powershell or any application language or MS Access or Excel with a DSN data source or SSIS or many many many other ways.

Context

StackExchange Database Administrators Q#320355, answer score: 8

Revisions (0)

No revisions yet.