sqlsql-serversql-server-2008

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?


Solution

  • In SSMS menu: Query > Results To > Results to File query-results-to

    Shortcut = CTRL+shift+F

    You can set it globally too

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

    Edit: after comment

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

    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