To narrow SQL Server log file SQL statements

Source: Internet
Author: User
Tags file size getdate log sql
server| statement
Paste the following code into SQL Query Analyzer (Note that the 3 parameters of the Red section are modified):

SET NOCOUNT ondeclare @LogicalFileName sysname, @MaxMinutes int, @NewSize int

Use Dicky-database name Select @LogicalFileName = ' Dicky_log ' that requires the log to be reduced--the logical name of the log file, @maxminutes =, non-physical file name--Limit on time Allowe D to wrap log. @NewSize = 2-the size of the log file that needs to be reset (in M), this value must be smaller than the original file size

--Setup/initializedeclare @OriginalSize intselect @OriginalSize = size from Sysfileswhere name = @LogicalFileNameSELECT ' Original Size of ' + db_name () + ' LOG is ' + CONVERT (VARCHAR, @OriginalSize) + ' 8K pages or ' + CONVERT (VARCHAR (30) , (@OriginalSize *8/1024)) + ' MB ' from sysfileswhere name = @LogicalFileNameCREATE TABLE Dummytrans (dummycolumn char (8000) NOT null)

DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR (255) SELECT @StartTime = GETDATE (), @TruncLog = ' BACKUP LOG ' + D B_name () + ' with Truncate_only '

DBCC shrinkfile (@LogicalFileName, @NewSize) EXEC (@TruncLog)-Wrap the log if necessary. While @MaxMinutes > DATEDIFF (MI, @StartTime, GETDATE ())--time has not expiredand @OriginalSize = (SELECT size from S Ysfiles WHERE name = @LogicalFileName) and (@OriginalSize * 8/1024) > @NewSize BEGIN--Outer loop. SELECT @Counter = 0WHILE ((@Counter < @OriginalSize/16) and (@Counter < 50000)) BEGIN--Updateinsert Dummytrans VA Lues (' Fill Log ') DELETE dummytransselect @Counter = @Counter + 1END EXEC (@TruncLog) End SELECT ' Final Size of ' + Db_nam E () + ' LOG is ' +convert (VARCHAR, size) + ' 8K pages or ' + CONVERT (VARCHAR), (size*8/1024) + ' MB ' from Sysfiles whe RE name = @LogicalFileNameDROP TABLE dummytransset NOCOUNT off


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.