2014-01-22 71 views
0

我有一個sql存儲過程,我使用sp_OAMethod從windows文件系統中刪除文件。當我們使用sql server 2005的時候,這用於正常工作,但是現在在使用sql server 2008 R2時根本無法工作。我已經讀過,你可以使用SQLDMO/SQLCLR,但是我找不到任何有關這些方法的體面信息。我以前的代碼如下:sp_OAMethod'DeleteFile'方法不能在SQL Server 2008 R2中工作

-- declare variables 
declare  @ObjectID  nvarchar(10), 
      @ObjectType  nvarchar(255), 
      @BackupName  nvarchar(255), 
      @BackupLocation nvarchar(255), 
      @ExpiryDate  datetime, 
      @DeletedStatus bit, 
      @SQL   nvarchar(4000), 
      @SQL1   nvarchar(4000), 
      @SQL2   nvarchar(4000), 
      @Result   int, 
      @FSO_Token  int, 
      @FileLocation nvarchar(4000) 

-- declare cursor for table backups 
declare backupexpired_cursor cursor for 
select dbo.tbl_BackupObjects.ObjectID, dbo.tbl_BackupObjects.ObjectType, dbo.tbl_BackupObjects.BackupName, 
    dbo.tbl_BackupObjects.BackupLocation, dbo.tbl_BackupObjects.ExpiryDate, dbo.tbl_BackupObjects.Deleted 
from dbo.tbl_BackupObjects 
where dbo.tbl_BackupObjects.Deleted <> 1 

-- open cursor 
open backupexpired_cursor 

-- fetch the next record from the cursor 
fetch next from backupexpired_cursor into @ObjectID, @ObjectType, @BackupName, @BackupLocation, @ExpiryDate, @DeletedStatus 

while (@@FETCH_STATUS <> -1) 
begin 
    if (@@FETCH_STATUS <> -2) 
    begin 
     if (@ExpiryDate < GetDate()) 
     begin 
      if (@ObjectType = 'Table') 
      begin 
       begin try 
        begin transaction 
         -- Only done if the object type is a table object 
         -- Remove old backup 
         select @SQL = 'drop table dbo.' + quotename(@BackupName) 
         exec sp_executesql @SQL 
         -- update the deleted status and the date deleted of the deleted object 
         select @SQL1 = 'update tbl_BackupObjects 
             set Deleted = 1, 
             DeletedDate = GetDate() 
             where ObjectID = ''' + @ObjectID + '''' 

         exec sp_executesql @SQL1 

        commit transaction 
       end try 
       begin catch 
        rollback transaction 

        select @SQL1 = 'update tbl_BackupObjects 
           set Deleted = 0, 
           DeletedDate = NULL 
           where ObjectID = ''' + @ObjectID + '''' 

        exec sp_executesql @SQL1 
       end catch 
      end 
      else 
      begin 
       begin try 
        begin transaction 
         -- Only done if the object(view, stored procedure, and/or function is saved 
         -- in a file located on the windows file system. 
         -- Create File Location 
         set @FileLocation = 'G:\Backup Registry Script Files\' + @BackupLocation + '\' + @BackupName + '' 

         -- Create a token of the object 
         EXEC @Result = sp_OACreate 'Scripting.FileSystemObject', @FSO_Token OUTPUT 

         -- Call the deletefile method using the @FileLocation parameter and the token created above: 
         --  - The object token created by sp_OACreate 
         --  - The method name 
         --  - The method's return value 
         --  - Parameters that will be used by the object method 
         EXEC @Result = sp_OAMethod @FSO_Token, 'DeleteFile', NULL, @FileLocation 

         -- Execute ole method 
         EXEC @Result = sp_OADestroy @FSO_Token  
         -- update the deleted status and the date deleted of the deleted object 


         select @SQL1 = 'update tbl_BackupObjects 
             set Deleted = 1, 
             DeletedDate = GetDate() 
             where ObjectID = ''' + @ObjectID + '''' 

         exec sp_executesql @SQL1 

        commit transaction 
       end try 
       begin catch 
        rollback transaction 

        select @SQL1 = 'update tbl_BackupObjects 
           set Deleted = 0, 
           DeletedDate = GetDate() 
           where ObjectID = ''' + @ObjectID + '''' 

        exec sp_executesql @SQL1 
       end catch 
      end 
     end 
    end 
    -- fetch the next record from the cursor 
    fetch next from backupexpired_cursor into @ObjectID, @ObjectType, @BackupName, @BackupLocation, @ExpiryDate, @DeletedStatus 
end 

-- set the Last and Next Removal Dates 
select @SQL2 = 'update tbl_BackupRemovalDate 
       set LastRemovalDate = GetDate(), 
        NextRemovalDate = GetDate() + 7' 
exec sp_executesql @SQL2 

-- close cursor 
close backupexpired_cursor 
deallocate backupexpired_cursor 

我已經看到,SQLDMO頗爲相似,我有什麼,但是我無法找到如何使用這種方法來刪除一個文件系統文件中的任何信息。誰能幫忙?

+0

你爲什麼要使用SP_OA這個呢? SQLCLR是你的朋友。 –

+0

正如我上面的問題所述:這是我的舊存儲過程,當我的工作安裝了SQL Server 2005時,我正在使用它。你的評論根本沒有幫助。我需要關於如何使用SQLCLR/SQLDMO的信息。如果你不能幫助,請不要回復我的問題。 – Cherry

+2

有一個原因,你看到的幾乎所有使用'sp_OA ...'過程的示例代碼都會檢查它們返回的內容。您的代碼始終忽略「@ Result」。直到你開始檢查和報告這些值(並且在適當的時候調用'sp_oageterrorinfo'),我們就沒有必要猜測*出了什麼問題。 –

回答

0

您是否有Enable Ole Automation Procedures功能?

試試這個

EXEC sp_configure 'show advanced options', 1 
RECONFIGURE 
EXEC sp_configure 'Ole Automation Procedures', 1 
RECONFIGURE 
+0

感謝您的回覆。但是,我已經啓用了ole自動化程序,但沒有成功。我想我需要改變我的過程來使用新的方法之一(SQLDMO/SQLCLR),但我不知道如何?如果你能說出一些很好的話。 – Cherry

+0

但我在我的SQL Server 2012中使用sp_OAMethod。它可以刪除文件的成功,也許你需要檢查@FileLocation路徑是正確的,並檢查目標文件夾的用戶權限是否正確,我的目標文件夾有每個人的用戶permission.maybe你需要添加一個SQLserver用戶 –

+0

好的謝謝,我會試試這個和讓你知道我是怎麼走的。 – Cherry