Tuesday, December 9, 2008

Reducing the size of a SQL transaction log

I used Microsoft SQL Server quite a bit and have been stuck numerous times on an ever growing transaction log.  After much pounding on the server this evening I once again found the magic commands.  The commands are executed in Query Analyzer or Management Studio after a full backup of the database.

   1:  BACKUP LOG <database_name> WITH TRUNCATE_ONLY
   2:  DBCC SHRINKFILE(<logical file name>,0)

With a command line backup:

   1:  BACKUP DATABASE <database_name> TO "<path to backup location>"
   2:  BACKUP TRAN <database_name> TO "path to backup location>"
   3:  DBCC SHRINKFILE 0, TRUNCATEONLY
   4:  BACKUP LOG <database_name> WITH TRUNCATE_ONLY
   5:  DBCC SHRINKFILE('<logical file name>',0)

You can get the actual database name and logical file names by examining the contents of the sysfiles table:

   1:  SELECT * FROM SYSFILES

No comments:

Post a Comment