in SqlServer management studio, you can export query results to a csv file by right click -> "Save Results as" -> csv. However, the csv file does not contain column headers and does not escape any commas in the data itself, which often leads to a malformed file.
How can I export query results with headers and commas properly handled?
Best Answer
See Options > Query Results > SQL Server > Results to Text
Check "include column headers". My text results are being enclosed in doublequotes so that should take care of the comma issue. Take a look at the "Results to grid" tab as well - I see a "Quote strings containing list separators when saving .csv results"
If not through SSMS then you can also use the sqlcmd command line utility which includes more options than the powershell equivalent.