2012-03-22 61 views
2

我已經在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 

回答

1

文件操作或其他殼操作難以從TSQL因爲沒有內置對他們的支持和權限是一個常見的問題(SQL Server服務帳戶可能沒有權限訪問文件系統)。

最簡單的解決方案就是用您的首選語言編寫獨立腳本;不是基於Access的腳本,因爲微軟使用Office recommends against服務器自動化。然後將其安排爲SQL Server作業或使用Windows調度程序。不幸的是,您沒有提及您的SQL Server版本或版本,因此不清楚您的服務器具有哪些功能(例如,Express Edition不包含SQL代理,因此沒有計劃的作業)。

+0

SS2k5 Full,Office2007,MSServer2k8 – 2012-03-23 12:57:34

+0

好的,那麼你可以在SQL Server中創建一個預定的作業來運行你的腳本,你也可能想看看維護計劃。但最簡單的解決方案仍然是一個獨立的腳本。 – Pondlife 2012-03-23 13:01:57