-------------------------------循环数据库压缩日志文件---------------------------

--循环数据库压缩日志文件(假设日志文件fileid=2)
create table #db(id int identity(1,1),name varchar(80),dbsize int,mdfsize int,ldfsize int)
insert into #db
 select a.name,SUM(b.size)*8/1024 as 'dbsize[MB]',
  SUM(case when b.type<>1 then b.size else 0 end)*8/1024 as 'datafilesize[MB]',
  SUM(case when b.type=1 then b.size else 0 end)*8/1024 as 'logfilesize[MB]'
 from sys.databases as a
  inner join sys.master_files as b on a.database_id=b.database_id
 where a.database_id>4
 group by a.name
 order by SUM(case when b.type=1 then b.size else 0 end)--按日志从大到小排序
declare @min_id int,@max_id int,@tmp_db varchar(80),@s varchar(max)
select @min_id=min(id),@max_id=max(id) from #db where ldfsize>0--此处指定日志文件大于多少M的
while @min_id< =@max_id
 begin
  select @tmp_db=name,@min_id=@min_id+1 from #db where id=@min_id
  set @s='BACKUP LOG '+@tmp_db+' with no_log'
  exec(@s)
  set @s='use '+@tmp_db+';dbcc shrinkfile(2)'
  exec(@s)
 end
 drop table #db
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值