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:
- Table_name: the name of the table from which data should be exported.
- The path and name of your CSV file is located at C:\your_folder\your_file.csv.
- -C: Output by character.
- Specify the field separator as a comma.
- – T: Log in to SQL Server using Windows authentication.
- 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.