SQL 维护计划(自动删除备份文件)

--开启修改配置功能 exec sp_configure 'allow updates', 0

--启用功能 sp_configure 'xp_cmdshell',1; reconfigure

--临时表 create table #temp (filesname varchar (5000 ), d int , f int ) --备份路径(需要将此文件夹的权限放开) declare @path varchar(1000) set @path='D:\DWDBBackup'   --文件后缀名 declare @extent varchar(1000) set @extent='abf'

--获取所有文件 insert into #temp Exec  master..xp_dirtree   @path,1,1

 

select * into #temp1 from #temp  where filesname like '%.'+@extent+'%' order by filesname desc

select * into  #temp2 from #temp1 where filesname not in (  select top 3 filesname from #temp1 order by filesname desc--保留3个文件 ) declare @count int select  @count=count(1) from #temp2

while @count>0 begin   declare @filename varchar(5000)    select top 1 @filename=filesname from #temp2   declare @sql varchar(5000)   select @sql='Del '+@path+'\'+@filename   Exec  master..xp_cmdshell @sql   delete from #temp2 where filesname=@filename   print @filename   select  @count=count(1) from #temp2 end

drop table #temp drop table #temp1 drop table #temp2

 

转载于:https://www.cnblogs.com/ifreesoft/p/3448539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值