Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a SELECT ... INTO OUTFILE equivalent in SQL Server Management Studio?

MySQL had a nifty command SELECT ... INTO OUTFILE that could write the result set into a file (CSV format or some other optional format).

I am currently using SQL Server Management Studio to query an MS-SQL backend server. I have multiple SQL queries and would like to write the output result set into a file. Is there any way I could store the results from a query directly into a file?

like image 395
Legend Avatar asked Jun 15 '11 06:06

Legend


People also ask

How do I export SQL query result to text file in SQL Server?

However, if you prefer to export SQL query results to a text file via a Wizard, we have your back. To begin with, right-click the database in SQL Server Management Studio or SSMS. Then, select the Import or Export data option and head to Export Data under Tasks. Next, open the SQL Server Import and Export wizard.

What can I use instead of like in SQL?

You may come across the situation, where you need alternate to “like” keyword of SQL, that is to search for sub-string in columns of the table. The one way to achieve it to use instr() function, instr() function takes 3 parameters in account .


1 Answers

In SSMS, "Query" menu item... "Results to"... "Results to File"

Shortcut = CTRL+shift+F

You can set it globally too

"Tools"... "Options"... "Query Results"... "SQL Server".. "Default destination" drop down

Edit: after comment

In SSMS, "Query" menu item... "SQLCMD" mode

This allows you to run "command line" like actions.

A quick test in my SSMS 2008

:OUT c:\foo.txt SELECT * FROM sys.objects 

Edit, Sep 2012

:OUT c:\foo.txt SET NOCOUNT ON;SELECT * FROM sys.objects 
like image 134
gbn Avatar answered Oct 04 '22 12:10

gbn