We’ve been migrating some of our production databases at work to SQL 2008 and I’m finding that Microsoft changed a lot. They updated the BACKUP LOG t-sql command and removed the TRUNCATE_ONLY switch, which means quickly and easily purging the transaction log requires a bit more work. I spent a little while yesterday and wrote the following T-SQL to purge the logs.
------------------------------------------------------------------------------
-- Otto R. Radke - http://ottoradke.com
-- Info: T-SQL script to shrink a database's transaction log. Just set the
-- database name below and run the script and it will shrink the
-- transaction log.
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Update the line below with the name of the database who's transaction
-- log you want to shrink.
------------------------------------------------------------------------------
USE YourDatabaseName
------------------------------------------------------------------------------
-- Don't change anything below this line.
------------------------------------------------------------------------------
GO
-- Declare variables
DECLARE @SqlStatement as nvarchar(max)
DECLARE @LogFileLogicalName as sysname
-- Alter the database to simple recovery
SET @SqlStatement = 'ALTER DATABASE ' + DB_NAME() + ' SET RECOVERY SIMPLE'
EXEC ( @SqlStatement )
-- Make sure it has been altered
SELECT [name], [recovery_model_desc] FROM sys.databases WHERE [name] = DB_NAME()
-- Set the log file name variable
SELECT @LogFileLogicalName = [Name] FROM sys.database_files WHERE type = 1
-- Shrink the logfile
DBCC Shrinkfile(@LogFileLogicalName, 1)
-- Alter the database back to FULL
SET @SqlStatement = 'ALTER DATABASE ' + DB_NAME() + ' SET RECOVERY FULL'
EXEC ( @SqlStatement )
-- Make sure it has been changed back to full
SET @SqlStatement = 'SELECT [name], [recovery_model_desc] FROM ' + DB_NAME() + '.sys.databases WHERE [name] = ''' + DB_NAME() + ''''
EXEC ( @SqlStatement )
------------------------------------------------------------------------------
If you want a copy of the source, let me know and I’ll either update this post with the source or email it to you.
{ 18 comments… read them below or add one }
Thank you. Your help was much faster and precise than anyone elses. Very much appreciated.
Works Fine. Thank You.
Excellent work!
Thank you!
Thank you! I have looked for days for this information, and I appreciate that you posted it for others.
Good work.
Thank you
Hi,
Thanks for the post.
can you help me with some thing? thanks in advance
we have transaction servers from which data is replicated to report db on different server from which reports are generated. now we are planning to purge the customer.employer etc data from the transaction servers itself instead of reporting server in real time.
how can we do that?
Awesome script. Thank you.
Fantantic, it worked even for me! First try. You are amazing.
Our company can not afford disk space and I freed up 100gig is a matter of seconds in our dev/test environments!
Danny
Otto,
Many thanks for this post & script. Saved my skin.
-Drew-
Thanks Otto.
Great script! Thanks
thank you.. work nice
thanks a lot
Great post!
thanks a lot.
I need some script to purge logfile daily
Thanks very much
nice post, but shrinking your logfiles is not a good practice.
Thanks a lot! It works fine! Great job!
thanks, this helped me