使用 SQL Server Management Studio 自动导出到 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9666362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:48:20  来源:igfitidea点击:

Automated Export to CSV Using SQL Server Management Studio

sqlsql-serversql-server-2008

提问by tob88

Using Microsoft SQL Server Management Studio, I have created a view, which pulls in columns from several tables. I need to export this view into a CSV file on a weekly basis, and so I would like to set up some sort of automated process for this. I have read many examples of how I can do a simple right click and "Save Results As", or using the export wizard, but I do not know how I can automate this process to run weekly.

我使用 Microsoft SQL Server Management Studio 创建了一个视图,该视图从多个表中提取列。我需要每周将此视图导出到 CSV 文件中,因此我想为此设置某种自动化流程。我已经阅读了许多关于如何执行简单的右键单击并“将结果另存为”或使用导出向导的示例,但我不知道如何自动执行此过程以每周运行一次。

I am somewhat of a newbie with all things microsoft, so any help is much appreciated, thanks!

我对微软的所有事情都有些陌生,所以非常感谢任何帮助,谢谢!

回答by Denis Valeev

It is also easy to setup a mailer that would mail a csv file as an attachment on a weekly basis using sp_send_dbmailcommand.

设置一个邮件程序也很容易,该邮件程序每周使用sp_send_dbmail命令将 csv 文件作为附件邮寄。

As a CSV attachment:

作为 CSV 附件:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'AdventureWorks2008R2 Administrator',
    @recipients = '[email protected]',
    @query = 'SELECT COUNT(*) FROM AdventureWorks2008R2.Production.WorkOrder
                  WHERE DueDate > ''2006-04-30''
                  AND  DATEDIFF(dd, ''2006-04-30'', DueDate) < 2' ,
    @subject = 'Work Order Count',
    @attach_query_result_as_file = 1 ;

Source: http://msdn.microsoft.com/en-us/library/ms190307.aspx

来源:http: //msdn.microsoft.com/en-us/library/ms190307.aspx

回答by Kaf

What you need is to schedule a job to run every week. Please have a look at here http://msdn.microsoft.com/en-us/library/ms191439.aspx

您需要的是安排每周运行的作业。请看这里http://msdn.microsoft.com/en-us/library/ms191439.aspx

回答by Matten

You can create an SSIS package (just google "sql server export data ssis") and execute it each week (you can create an automated task for this) or you can use xp's like demonstrated in this article.

您可以创建一个 SSIS 包(只是谷歌“sql server export data ssis”)并每周执行它(您可以为此创建一个自动化任务),或者您可以像本文中演示的那样使用 xp