SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
@MaxMinutes INT,
@NewSize INT
USE TPMS-- 要操作的数据库名
SELECT @LogicalFileName = 'TPMS_LOG', -- 日志文件名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 10 -- 你想设定的日志文件的大小(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 has not expired
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
出错信息:
消息 208,级别 16,状态 1,第 42 行
对象名 'DummyTrans' 无效。
我以前用的是sql2000没有问题,
最近升级到sql2008后,执行出错。
那位大虾帮帮忙啊!!
谢谢!!!
------解决方案--------------------
- SQL code
(SQL2008):在SQL2008中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式。USE [master] GO ALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE DNName SET RECOVERY SIMPLE --简单模式 GO USE DNName GO DBCC SHRINKFILE (N'DNName_Log' , 11, TRUNCATEONLY) GO USE [master] GO ALTER DATABASE DNName SET RECOVERY FULL WITH NO_WAIT GO ALTER DATABASE DNName SET RECOVERY FULL --还原为完全模式 GO优点:此清除日志所运行消耗的时间短,90GB的日志在分钟左右即可清除完毕,做完之后做个完全备份在分钟内即可完成。缺点: 不过此动作最好不要经常使用,因为它的运行会带来系统碎片。普通状态下LOG和DIFF的备份即可截断日志。此语句使用的恰当环境:当系统的日志文件异常增大或者备份LOG时间太长可能影响生产的情况下使用。