I need to somehow export the result of my SQL query to a CSV file. However, it has to be directly via SQL or VBS.
Or, you can export all the data from the database table to CSV.
I need to somehow export the result of my SQL query to a CSV file. However, it has to be directly via SQL or VBS.
Or, you can export all the data from the database table to CSV.
The BCP utility enables you to export data to a text file, CSV format. With the -t option you can define the field separator. I recommend pre-reading Specifying field and line terminators .
You can also use the OPENROWSET function to export.
Some articles on the subject:
I was able to export from SQL Server to CSV using the code below:
exec master..xp_cmdshell 'bcp "Select E3TimeStamp, Message, USUARIO, Acked, Severity, FormattedValue, Area, EventType From [c1_alarmes].[dbo].[table_alarmes]" queryout D:\bcp_outputTable.CSV -c -t; -T -S localhost\Testes'
The same generated the CVS file after enabling xp_cmdshell through the command:
exec sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
A question I still have, in the file generated does not show the name of each column. If anyone knows how to add this to the generation.