sp_OAMethod'DeleteFile'方法在SQL Server 2008 R2中不起作用

时间:2014-01-22 00:44:13

标签: sql sql-server sql-server-2008-r2

我有一个sql存储过程,我用它来使用sp_OAMethod从windows文件系统中删除文件。这在我们使用sql server 2005时工作得很好,但是,现在使用sql server 2008 R2时根本不起作用。我已经读过您可以使用SQLDMO / SQLCLR但是,我找不到任何关于这些方法的正确信息。我之前的代码如下:

-- 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与我的相似,但我找不到任何有关如何使用此方法删除文件系统文件的信息。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您是否拥有Enable Ole Automation Procedures功能?

试试这个

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