How to export data as CSV format from SQL Server using sqlcmd?

后端 未结 11 1768
天涯浪人
天涯浪人 2020-11-22 13:06

I can quite 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\"
         


        
11条回答
  •  一生所求
    2020-11-22 13:44

    Since following 2 reasons, you should run my solution in CMD:

    1. There may be double quotes in the query
    2. Login username & password is sometimes necessary to query a remote SQL Server instance

      sqlcmd -U [your_User]  -P[your_password] -S [your_remote_Server] -d [your_databasename]  -i "query.txt" -o "output.csv" -s"," -w 700
      

提交回复
热议问题