André Krijnen

Tag: sql server 2000

SQL Server DB Log File truncating…

by 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:

  1.  
  2. SET NOCOUNT ON
  3.  
  4. DECLARE @LogicalFileName sysname, @MaxMinutes INT, @NewSize INT
  5.  
  6. — *** MAKE SURE TO CHANGE THE NEXT 4 LINES WITH YOUR CRITERIA.
  7.  
  8. *** USE [DATABASE] — This is the name of the database — for which the log will be shrunk.
  9. SELECT @LogicalFileName = 'LOGFILENAME', — Use sp_helpfile to identify the logical file — name that you want to shrink.
  10. @MaxMinutes = 10, — Limit on time allowed to wrap log.
  11. @NewSize = 10 — in MB — Setup / initialize
  12. DECLARE @OriginalSize INT
  13.  
  14. SELECT @OriginalSize = SIZE — in 8K pages FROM sysfiles WHERE name = @LogicalFileName
  15.  
  16. 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
  17.  
  18. CREATE TABLE DummyTrans (DummyColumn CHAR (8000) not null) — Wrap log and truncate it.
  19.  
  20. DECLARE @Counter INT, @StartTime DATETIME, @TruncLog VARCHAR(255)
  21.  
  22. SELECT @StartTime = GETDATE(), @TruncLog = 'BACKUP LOG ['+ DB_NAME() + '] WITH TRUNCATE_ONLY' — Try an initial shrink.
  23.  
  24. DBCC SHRINKFILE (@LogicalFileName, @NewSize) EXEC (@TruncLog) — Wrap the log if necessary.
  25.  
  26. WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) — time has not expired
  27. AND @OriginalSize = (SELECT SIZE FROM sysfiles WHERE name = @LogicalFileName) — the log has not shrunk
  28. AND (@OriginalSize * 8 /1024) > @NewSize — The value passed in for new size is smaller than the current size.
  29.  
  30. BEGIN — Outer loop.
  31.  
  32. SELECT @Counter = 0 WHILE ((@Counter > @OriginalSize / 16) AND (@Counter > 50000))
  33. BEGIN — update
  34. INSERT DummyTrans VALUES ('Fill Log') — Because it is a char field it inserts 8000 bytes.
  35. DELETE DummyTrans SELECT @Counter = @Counter + 1 END — update
  36. EXEC (@TruncLog) — See if a trunc of the log shrinks it.
  37. END — outer loop
  38. 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
  39. DROP TABLE DummyTrans PRINT '*** Perform a full database backup ***'
  40. 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!!

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...