Export SQL Server query to CSV file

Asked

Viewed 14,296 times

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 else, all database table data can be exported to CSV.

3 answers

3


2

I was able to export from SQL Server to CSV via 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'

It generated the CVS file after enabling xp_cmdshell via the command:

exec sp_configure 'show advanced options', 1 
RECONFIGURE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE

A question I still have, in the generated file does not show the name of each column. If someone knows how to add this in generation.

1

With Sqlserver, after performing the query, go to the result and right-click: inserir a descrição da imagem aqui

The first option of formed to save will be .csv.

  • 1

    But I needed to do just that through code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.