How to generate a csv file using select query in SQL [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-20 12:29:33

问题


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

I want to generate CSV file using select query in SQL-server.

The code below is working correctly in mySQL:

select * into outfile 'd:/report.csv' fields terminated by ',' from tableName;

It generated the CSV file.

Does anybody know how can I create a CSV file using select query in SQL-server?


回答1:


Will this do the work

sqlcmd -S server -U loginid -P password -d DBname -Q "select * from tablename" -o output.csv

EDIT:

Use -i options if you want to execute a SQL script like -i sql_script_filename.sql




回答2:


SQLCMD -S MyInstance -E -d sales -i query_file.sql -o output_file.csv -s

You can use OPENROWSET() to read from a CSV file within a T-SQL query but AFAIK you can't write to one. This is really what SSIS/DTS is for.

If you're working with it interactively in SQL Server Management Studio you could export a grid to a file.



来源:https://stackoverflow.com/questions/6776883/how-to-generate-a-csv-file-using-select-query-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!