从SQL Server代理作业存档的Zip文件
问题描述:
我已经在Access vba中编写了一个程序,该程序转到了一个固定目录,将该目录(和子目录)中的所有文件压缩,并将zip存档放入(通常)新的目录存档。这是我的Access前端和相关文件的备份例程。我每天都使用我的个人凭证从我的客户端计算机上运行它。现在我想从我的小型SQL Server机器上运行它,因为它更健壮,更负责任。我宁愿在T-SQL中完成所有工作,而不是“调用”Access例程。从SQL Server代理作业存档的Zip文件
我已经做了一些研究,但找不到任何明确的,将帮助我与此。有人能指点我一些帮助吗?下面是访问VBA代码:从外壳
Function Zip_All_Files_in_Folder()
Dim FileNameZip, FolderName
Dim strDate As String, TargetPath As String
Dim oApp As Object
TargetPath = "H:\xxx\secure\Construction\Access\All Database Backup\" & Format(Date, "YYYY-MMM") & "_Backup\"
If Len(Dir(TargetPath)) = 0 Then
MkDir (TargetPath)
End If
FolderName = "H:\xxx\secure\Construction\Access\CPAS\"
strDate = Format(Date, "YY-MM-DD")
FileNameZip = TargetPath & strDate & ".zip"
'Create empty Zip File
NewZip (FileNameZip)
'Copy the files to the compressed folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameZip).CopyHere oApp.Namespace(FolderName).items
'Keep script waiting until Compressing is done
On Error Resume Next
Dim time As Integer
Do Until (oApp.Namespace(FileNameZip).items.Count = _
oApp.Namespace(FolderName).items.Count) Or time > 180
Sleep (1000)
time = time + 1
Loop
On Error GoTo 0
'Send a message about the way the script ended
If time < 180 Then
SendEmail "[email protected]", "Looks like the zip backup worked." & vbCrLf & TargetPath
Else
SendEmail "[email protected]", "Better double check the Zip backup: " & time & " seconds" & vbCrLf & TargetPath
End If
DoCmd.Quit
End Function
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
答
文件操作或其他壳操作难以从TSQL因为没有内置对他们的支持和权限是一个常见的问题(SQL Server服务帐户可能没有权限访问文件系统)。
最简单的解决方案就是用您的首选语言编写独立脚本;不是基于Access的脚本,因为微软使用Office recommends against服务器自动化。然后将其安排为SQL Server作业或使用Windows调度程序。不幸的是,您没有提及您的SQL Server版本或版本,因此不清楚您的服务器具有哪些功能(例如,Express Edition不包含SQL代理,因此没有计划的作业)。
SS2k5 Full,Office2007,MSServer2k8 – 2012-03-23 12:57:34
好的,那么你可以在SQL Server中创建一个预定的作业来运行你的脚本,你也可能想看看维护计划。但最简单的解决方案仍然是一个独立的脚本。 – Pondlife 2012-03-23 13:01:57