当前位置: 代码迷 >> Sql Server >> sqlserver2008 截断指定时间先前的事务日志的命令
  详细解决方案

sqlserver2008 截断指定时间先前的事务日志的命令

热度:471   发布时间:2016-04-24 20:19:17.0
sqlserver2008 截断指定时间以前的事务日志的命令。
如题。 想每隔一个月截断上一个月的事务日志。
如:3月底删掉2月整个月的事务日志。
求截断指定时间以前的事务日志的方法。

------解决方案--------------------
试试:
-- use the cursor to delete the expire bak

-- also could not user the cursor if you only want to delete the top 1 oldest bak

-- @filePath : the expire bak's path

declare fileCursor CURSOR for 

SELECT * from

(

SELECT TOP 3 b.physical_device_name    

FROM [msdb].[dbo].[backupset] a,[msdb].[dbo].[backupmediafamily] b  

where a.media_set_id=b.media_set_id  and [expiration_date]<GETDATE()

order by [expiration_date] asc



as filetable

declare @filePath varchar(100)  

open  fileCursor  

fetch next from fileCursor into @filePath  

while @@fetch_status=0  

begin  

declare @delCmd varchar(100)

set @delCmd =('del '+@filePath)

-- user xp_cmdshell to delete the bak

exec xp_cmdshell @delCmd

fetch next from fileCursor into @filePath 

end  

close fileCursor  

deallocate fileCursor
  相关解决方案