Chinaunix首页 | 论坛 | 博客
  • 博客访问: 771293
  • 博文数量: 803
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-28 10:29
文章分类

全部博文(803)

文章存档

2011年(1)

2008年(802)

我的朋友

分类:

2008-10-29 11:44:52


  SET NOCOUNT ON
  DECLARE @LOGICALFILENAME SYSNAME,
      @MAXMINUTES INT,
      @NEWSIZE INT
  USE abc---要操作的数据库
  SELECT @LOGICALFILENAME = 'abc_LOG',---日志文件名
      @MAXMINUTES = 10,---LIMIT ON TIME ALLOWED TO WRAP LOG.
      @NEWSIZE = 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 @COUNT 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 NOE EXPIRED
   AND @ORIGINALSIZE =(SELECT SIZE FROM SYSFILES WHERE NAME = @LOGICALFILENAME)
   AND (@ORIGINALSIZE*8/1024)>@NEWSIZE
  BEGIN --OUTER LOOP.
    SELECT @COUNT = 0
      WHILE((@COUNT<@ORIGINALSIZE/16) AND (@COUNT<50000))
      BEGIN -- UPDATE
        INSERT DUMMYTRANS VALUES ('FILL LOG')
        DELETE DUMMYTRANS
        SELECT @COUNT = @COUNT + 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
【责编:admin】

--------------------next---------------------

阅读(331) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~