Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in SQL by (6.1k points)

I can pretty easily dump data into a text file such as:

sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
     -o "MyData.txt"

Though I have looked at the help files for SQLCMD, I have not seen an option, especially for CSV.

Is there a way to dump the data from a table into a CSV text file using SQLCMD?

1 Answer

0 votes
by (12.7k points)

You can execute something like below:

sqlcmd -S MyServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
       -o "MyData.csv" -h-1 -s"," -w 700
  • -h-1 removes the column name headers from the result
  • -s"," set the column separator to,
  • -w 700 sets the row width to 700 chars (this will have to be as wide as the longest row or it would wrap to the next line)

 Want to know more about SQL ? Join this SQL Course by Intellipaat.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...