简体   繁体   English

自动将SQL查询导出到csv或txt文件

[英]Automatically export SQL query to a csv or txt file

I'm currently using SSRS to export reports. 我目前正在使用SSRS导出报告。 However my client would like to have a url to download or file dynamically produced possibly on a schedule or on command. 但是,我的客户希望有一个URL,以根据时间表或命令动态下载或动态生成文件。

What would be the best way of doing this without trying to force SSRS to do more than it can? 在不试图强迫SSRS做更多事情的情况下,这样做的最佳方法是什么?

Is there a simple way. 有没有简单的方法。 I am new to SQL. 我是SQL新手。

Why not use the subscription and delivery services within SSRS ( http://technet.microsoft.com/en-us/library/ms159762.aspx )? 为什么不使用SSRS( http://technet.microsoft.com/zh-cn/library/ms159762.aspx )中的订阅和传递服务? The nice thing about this is it will allow you to choose a number of convenient render options. 这样做的好处是,它将允许您选择许多方便的渲染选项。 You can have it email or send to a shared file location. 您可以通过电子邮件发送或将其发送到共享文件位置。 If the client is asking for this to be produced on demand, then wouldn't the export option within SSRS be sufficient? 如果客户要求按需生产,那么SSRS内的出口选择是否足够?

You can use this query : 您可以使用以下查询:

SQLCMD -S YourSQLServer -d YourDatabase -U YourUserName -P
YourPassword -Q “Your Query” -s “,” -o “C:\Yourfilename.csv”

Now you can go to your file location and open the file and you will see that new csv file created there. 现在,您可以转到文件位置并打开文件,您将看到在那里创建了新的csv文件。 When you open the csv file you will notice the results of the query. 当您打开csv文件时,您会注意到查询结果。

Check http://blog.sqlauthority.com/2013/03/05/sql-server-exporting-query-results-to-csv-using-sqlcmd/ this for more details. 有关更多详细信息,请参见http://blog.sqlauthority.com/2013/03/05/sql-server-exporting-query-results-to-csv-using-sqlcmd/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM