简体   繁体   English

将SQL Server 2012报表计划到SFTP

[英]Schedule SQL Server 2012 report to SFTP

I have a requirement to auto upload a report nightly and transfer using SFTP. 我需要每晚自动上传报告并使用SFTP进行传输。 I can create the report in SQL Server 2012 but how can I auto save it to a specific folder in a flat file format ( .csv / .txt )? 我可以在SQL Server 2012中创建报告,但是如何将其以平面文件格式( .csv / .txt )自动保存到特定文件夹中?

Once it is saved, it needs to be scheduled to upload and get transferred using SFTP. 保存后,需要安排其上载并使用SFTP进行传输。 Could someone please advise and assist me with it? 有人可以提出建议并协助我吗? Thank you so much. 非常感谢。

Update: Here is the extraction part. 更新:这是提取部分。 I created a .bat file that I have scheduled in windows task manager using SQLCMD. 我创建了一个使用SQLCMD在Windows任务管理器中计划的.bat文件。 Now I can schedule it to auto-upload using the WinSCP commands. 现在,我可以使用WinSCP命令将其计划为自动上载。 Just thought to update the answer for future users. 只是想为将来的用户更新答案。

SQLCMD -S Servername -d databasename -U username -P password -i C:\sample.sql -o C:\sampletemp.txt -s"|" -W
findstr /B /V /C:"----" "C:\sampletemp.txt" > "C:\finaloutput.txt"
del "C:\sampletemp.txt"

To setup SSIS SFTP task using WinSCP: 要使用WinSCP设置SSIS SFTP任务:

(I'm the author of WinSCP) (我是WinSCP的作者)

If you have SQL Server Standard or Enterprise, you have SSIS. 如果您具有SQL Server Standard或Enterprise,则具有SSIS。 If you have SQL Server Web or better (ie, not Express) then you have SQL Agent. 如果您具有SQL Server Web或更高版本(即,不是Express),则您具有SQL代理。

"How do I use SSIS?" “如何使用SSIS?” is a question somewhat beyond the scope of a question on Stack Overflow. 这个问题有点超出堆栈溢出问题的范围。 It's a full ETL package. 这是一个完整的ETL软件包。 Microsoft has some basic SSIS tutorials you can go through but it is a very deep system. Microsoft提供了一些基本的SSIS教程 ,但是它是一个非常深入的系统。

That's how you're supposed to do it. 那就是你应该做的。 How MS intends people to do it. MS如何让人们做到这一点。 You can also use the Import/Export Wizard, which lets you save an SSIS package at the end of the wizard for later use. 您也可以使用“导入/导出向导”,该向导使您可以在向导末尾保存SSIS包以供以后使用。 That won't help with scripting the WinSCP transfer, though. 但是,这对编写WinSCP传输脚本没有帮助。

If that feels like using a sledgehammer to drive a nail (or you're on SQL Express or Web) then the most basic way to do this would be to use a PowerShell script. 如果感觉像用大锤钉钉子(或者您在SQL Express或Web上),那么最基本的方法是使用PowerShell脚本。 In 2008/2008 R2 there's the SqlServerCmdletSnapin100 snap-in. 在2008/2008 R2中,有SqlServerCmdletSnapin100管理单元。 Beginning with 2012, that changed to the SQLPS module. 从2012年开始,将其更改为SQLPS模块。 Note that snap-ins ( Get-PSSnapin -Registered ) are different than modules ( Get-Module -ListAvailable ) but they otherwise function very similarly; 请注意,管理单元( Get-PSSnapin -Registered )与模块( Get-Module -ListAvailable )不同,但是它们的功能非常相似。 modules are just newer, AFAIK. 模块只是较新的AFAIK。 They both have Invoke-Sqlcmd . 他们都有Invoke-Sqlcmd You can also use .Net methods in System.Data.SqlClient if you're not installing SQL Server on the machine running the script, but that's a bit more involved. 如果您不打算在运行脚本的计算机上安装SQL Server,也可以在System.Data.SqlClient中使用.Net方法。

So you can then do something like: 因此,您可以执行以下操作:

#Add the relevant snap-in/module; for 2012 that's sqlps
#Add-PSSnapin SqlServerCmdletSnapin100;
Import-Module sqlps;

#Fetch the data
$Query = 'SELECT Field1, Field2, Field3 FROM MyView ORDER BY Field1;';
$Data = Invoke-Sqlcmd -Server MyServer -Database MyDatabase;

#Save data to file
$Data | Export-Csv -Path 'C:\Script\MyExportFile.csv' -NoTypeInformation;

#Run the WinSCP script to upload the data
#Note that --% is Powershell's "Stop parsing" operator; it keeps the parser from messing with the arguments.
&"C:\Program Files (x86)\WinSCP\WinSCP.exe" --% -console -script="C:\Script\WinSCPScript.txt" -log="C:\Script\Logs\WinSCP-!S-!Y-!M-!D-!T.log" -xmlgroups

Then you just have to code your WinSCP script file, and schedule the script with Windows Task Scheduler. 然后,您只需要编码WinSCP脚本文件,并使用Windows Task Scheduler安排脚本。

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

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