Tag: sql server 2000
SQL Server DB Log File truncating…
by mysticslayer on Jan.31, 2008, under maintenance, sql server
Well alot of database administrators forget to truncate their database daily before backing the database up. Well I don’t understand why they are forgetting it. Because it’s a simple procedure you can add before you backup the database. Today I received a database of 5 gigabytes and with a log file of yes you don’t how of 21 gigabytes. So I was using the standard features of SQL Server 2000, but this tool does not his work with the normal truncating options of the database.
After a good search I found out it isn’t that much fun to truncate the database with a procedure. After a search on google it was easy to find the simplest script for database maintenance. I used this script and it really did work. Below you can find out yourself:
-
-
SET NOCOUNT ON
-
-
DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT
-
-
— *** MAKE SURE TO CHANGE THE NEXT 4 LINES WITH YOUR CRITERIA.
-
-
*** USE [DATABASE] — This is the name of the database — for which the log will be shrunk.
-
SELECT @LogicalFileName = 'LOGFILENAME', — Use sp_helpfile to identify the logical file — name that you want to shrink.
-
@MaxMinutes = 10, — Limit on time allowed to wrap log.
-
@NewSize = 10 — in MB — Setup / initialize
-
DECLARE @OriginalSize INT
-
-
SELECT @OriginalSize = SIZE — in 8K pages 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) — Wrap log and truncate it.
-
-
DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR(255)
-
-
SELECT @StartTime = GETDATE(), @TruncLog = 'BACKUP LOG ['+ DB_NAME() + '] WITH TRUNCATE_ONLY' — Try an initial shrink.
-
-
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) — the log has not shrunk
-
AND (@OriginalSize * 8 /1024) > @NewSize — The value passed in for new size is smaller than the current size.
-
-
BEGIN — Outer loop.
-
-
SELECT @Counter = 0 WHILE ((@Counter > @OriginalSize / 16) AND (@Counter > 50000))
-
BEGIN — update
-
INSERT DummyTrans VALUES ('Fill Log') — Because it is a char field it inserts 8000 bytes.
-
DELETE DummyTrans SELECT @Counter = @Counter + 1 END — update
-
EXEC (@TruncLog) — See if a trunc of the log shrinks it.
-
END — outer loop
-
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 PRINT '*** Perform a full database backup ***'
-
SET NOCOUNT OFF
Voila, fill in the database name and the logfile name and you’re done. Run the script within query analyser or other usefull tool. I used this script with SQL Server 2000 and SQL Server 2005. This script can also be used with SQL Server 7. So with the major databases of Microsoft you can use this script. Have fun!!