数据库日志清除

set nocount on
declare @LogicalFileName sysname
declare @MaxMinutes int
declare @NewsSize int
use shopu
select @LogicalFileName='shopu_log.ldf',-----日志文件名
@MaxMinutes=10,---limit on time allowed to warp log.
@NewsSize=1    ---你想设定的日志文件的大小(M)
-- setup / initialize
declare @OriginalSize int
select @OriginalSize=size from sysfiles where name=@LogicalFileName
select 'Original size of'+db_name()+'LOG is'+CONVERT(varchar(30),@OriginalSize)+'8K pages or'+
convert(varchar(30),(@OriginalSize*8/1024))+'MB'
from sysfiles
where name=@logicalFileName
-------
create table DummyTrans
(DummyColumn char(8000) not null)
--------
    declare @Counter int,@StartTime datetime,@TruncLog varchar(255)
 select @StartTime=getdate(),@TruncLog='BACKUP LOG'+DB_name()+'WITH TRUNCATE_ONLY'
 DBCC SHRINKFILE(@LogicalFileName,@NewsSize)
 EXEC(@TruncLog)
----------------------------------------------------
--Wrap the log if necessary
    while (
 @MaxMinutes>DATEDIFF(mi,@StartTime,Getdate())
 and @OriginalSize=(select size from sysfiles where name=@LogicalFileName)
 and (@OriginalSize * 8/1024)>@NewsSize
 )
  begin
   select @Counter=0
   while((@Counter<@OriginalSize/16) and (@Counter<50000))
   begin       
    insert DummyTrans Values ('Fill log')
    delete DummyTrans
    select @Counter=@Counter+1
    end
   exec (@TruncLog)
 end
    select 'Final Size of'+db_name()+'LOG is'+
    convert(varchar(30),size)+'8K pages or'+convert(varchar(30),(size*8/1024))+'MB'
    from sysfiles
    where name=@LogicalFileName
 Drop Table DummyTrans
    set nocount off

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值