Search This Blog

Saturday, February 9, 2008

Shrinking a Log file

What if your database is in Full recovery mode and the T-logs are huge :D.....

This indicates that either you have not taken the backups since long or the backups are failing .In Full recovery the T-Logs keeps on growing untill you take the backups .This is not so in simple recovery mode ...

Now , if you try to shrink the database or the T-log file , it will not .Because there may be uncommitted or not backed up transactions at the bottom of the virtual log file ..

To check this run DBCC Loginfo command .You will see the last value in the status column as 2 ..
to make it 0 :

1) take full backup ...
2) change the database recovery model to simple

and then shrink the logfile DBCC SHRINKFILE (msdblog);

if you do not want to take any of the steps above try this :
repeatedly run the following script until
DBCC logingo show the last entry as having a status of '0' ('2' should be move to one of the first entries)

CREATE TABLE DummyTrans (DummyColumn char (8000) not null)
DECLARE @Counter int
SELECT @Counter = 1
WHILE @Counter < 1000
BEGIN
INSERT DummyTrans VALUES ('XYZ')
SELECT @Counter = @Counter + 1
END
DROP TABLE DummyTrans


USE UserDB
GO
DBCC SHRINKFILE (logfile name )
GO

Hope this helps ......

No comments: