Export query in SQL Server to CSV file

1

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.

    
asked by anonymous 04.04.2017 / 12:37

3 answers

3

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:

04.04.2017 / 12:54
2

With SQLServer, after performing the query, go to the result and right-click:

The first save option will be .csv .

    
04.04.2017 / 12:53
2

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.

    
04.04.2017 / 14:57