Monday, 9 February 2015

DB - SQL Server: Options for output the query resultset to file (e.g. csv)

Option 1: 
In SQL Server MicroSoft Studio

In SSMS, "Query" menu item... "Results to"... "Results to File"
or
Shortcut = CTRL+shift+F

Then in the query, type in the SQL query then execute it, the window will prop-up and ask for the output file.


Option2:
Use SQLCMD Command in windows cmd prompt:

- SQL Server authentication:
SQLCMD -U Tib_Dev -P Tib_Dev -S MASSQL02\Inst2 -Q "USE Tib_DEV_CLE select top 10 eventid, transactionid, eventdatetime, eventtype, hostname, projectname from dbo.event" -s "," -o "C:\MyData1.csv"


-Windows Authentication:
SQLCMD -S STETIBSQL01 -E -Q "USE Tib_PROD_REF select consignmentid from Consignmentv2ErrorReplay" -s "," -o "C:\MyData.csv"






No comments:

Post a Comment