Jun 30, 2016 06:17 AM|eralper|LINK
If you want to export SQL query results from SQL Server to text file in CSV format, please check
SQL Server BCP command for SQL output to file
Instead of SQLCMD you will be using BCP utility for data export to text file in csv format from SQL Server database query
I hope it helps
Here is how BCP sample looks like, please review the mentioned SQL tutorial for more detail
SET @sql = 'bcp "SELECT ....." queryout "c:\filename.csv" -c -UTF8 -T -Slocalhost'