用脚本缩小数据库日志

因为客户使用的数据库时常因为日志过大而导致硬盘空间不够,或者备份出来的文件太大无法通过邮件传送。

  闲下有余,参考SQLSERVER的帮助文件,写了如下脚本,可以截断日志,以达到缩小文件的目的。有空大家可以在自己的SQLSERVER上测试下效果哦。。。:)也许对有些情况导致的日志过大没有作用,这点可以同各位同仁互相交流下。

--在MASTER数据库中执行以下脚本(使用查询分析器)
declare @dbname varchar(50)
declare temp_cur cursor scroll for select name from sysdatabases
open temp_cur
fetch first from temp_cur into @dbname
while @@fetch_status =0
begin
  exec ('backup log '+@dbname+' with no_log')
  exec ('dbcc shrinkdatabase('+@dbname+')')
  exec ('dbcc checkcatalog ('+@dbname+')')
  exec ('dump transaction '+@dbname+' with no_log')
  fetch next from temp_cur into @dbname
end
close temp_cur
deallocate temp_cur

针对单个数据库,使用下面的语句就行了 ([1948] 为数据库名):
exec ('backup log [1948] with no_log')
  exec ('dbcc shrinkdatabase([1948])')
  exec ('dbcc checkcatalog ([1948])')
  exec ('dump transaction [1948] with no_log')
网易游戏各种序列号,新手卡免费领,QQ85579045
推荐倩女幽魂1888至尊序列号,大唐无双豪杰序列号,战歌无忧序列,大话西游2老朋友序列号