sqlcmd -S myServer -d myDB -E -o "MyData.txt" ^
-Q "select bar from foo" ^
-W -w 999 -s","
The last line contains CSV-specific options.
-W
remove trailing spaces from each individual field-s","
sets the column seperator to the comma (,) -w 999
sets the row width to 999 charsscottm's answer is very close to what I use, but I find the -W
to be a really nice addition: I needn't trim whitespace when I consume the CSV elsewhere.
Also see the MSDN sqlcmd reference. It puts the /?
option's output to shame.