欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据库清除日志文件(LDF文件过大)

程序员文章站 2023-12-01 09:29:58
清除日志: 复制代码 代码如下: declare @logicalfilename sysname, @maxminutes int, @newsize int use s...
清除日志:
复制代码 代码如下:

declare @logicalfilename sysname,
@maxminutes int,
@newsize int
use szwzcheck -- 要操作的数据库名
select @logicalfilename = 'szwzcheck_log', -- 日志文件名
@maxminutes = 10, -- limit on time allowed to wrap log.
@newsize = 20 -- 你想设定的日志文件的大小(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, @newsize)
exec (@trunclog)
-- wrap the log if necessary.
while @maxminutes > datediff (mi, @starttime, getdate()) -- time
and @originalsize = (select size from sysfiles where name =
@logicalfilename)
and (@originalsize * 8 /1024) > @newsize
begin -- outer loop.
select @counter = 0
while ((@counter < @originalsize / 16) and (@counter < 50000))
begin -- update
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

把szwzcheck换成你数据库的名字即可,在查询分析器里面运行。
有全角的空格(为了显示好看),你自己把他换一下.

编辑注:
一般情况下,用下面的语句更简单
复制代码 代码如下:

dump transaction [jb51] with no_log
backup log [jb51] with no_log
dbcc shrinkdatabase([jb51])

其中jb51就是你要处理的数据库名。