How can I export a CSV file from SQL Server?

In SQL Server Management Studio, you can export data to a CSV file by executing the following SQL statement:

EXEC xp_cmdshell 'bcp "SELECT * FROM your_table" queryout "C:\your_folder\your_file.csv" -c -t, -T -S your_server'

Among them:

  1. Table_name: the name of the table from which data should be exported.
  2. The path and name of your CSV file is located at C:\your_folder\your_file.csv.
  3. -C: Output by character.
  4. Specify the field separator as a comma.
  5. – T: Log in to SQL Server using Windows authentication.
  6. The instance name of your server is SQL Server.

Furthermore, to execute the SQL statement above, it is necessary to ensure that the xp_cmdshell option of SQL Server is enabled.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds