Shrink Database file

Script used to shrink, SQL 2008

 USE directory; GO

-- Truncate the log by changing the database recovery model to SIMPLE. ALTER DATABASE directory SET RECOVERY SIMPLE; GO

-- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (2, 1); -- here 2 is the file ID for trasaction log file,you can also mention the log file name dbname_log) GO

-- Reset the database recovery model. ALTER DATABASE directory SET RECOVERY FULL; GO

--Getting below error---
Script failed:Cannot shrink log file 2 (DirectoryLog) because the logical log file located at the end of the file is in use.
more ▼

asked Feb 08, 2012 at 08:50 PM in Default

subramanyam gravatar image

1 2 2 2

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

The quick answer is that the transaction log is made up of multiple smaller virtual log files (VLFs) and that transaction log usage is circular. This means that you would be unable to shrink the file because the latest VLF is in use. I'd recommend running a transaction and then performing a checkpoint to hopefully go to the next VLF.

For a proper and full explanation please read [Paul Randal's post on the matter][1]

Of interest, why are you shrinking the transaction log on this system? By switching recovery models you are losing your point in time recovery capability which may severely impact your recovery point objectives. Also, your transaction log will grow again. You should be taking regular transaction log backups to properly manage growth.

[1]: http://www.sqlskills.com/BLOGS/PAUL/post/Inside-the-Storage-Engine-More-on-the-circular-nature-of-the-log.aspx
more ▼

answered Feb 08, 2012 at 09:29 PM

SirSQL gravatar image

4.8k 1 3

(comments are locked)
10|1200 characters needed characters left

+1 to @SirSQL.

And I only add, if you ara swithing the recovery model to simple and than back into full, than it probably means, that you do not need the point in time recovery and then you can keep the the Simple recovery mode.

In case you really need the point in time recovery and FULL recovery mode, then schedule a periodic LOG backups. In case you have log backups scheduled, than probably the intarval between backups is too long and your log grows too much. Then you should shorten the intervals between log backups.
more ▼

answered Feb 09, 2012 at 11:00 AM

Pavel Pawlowski gravatar image

Pavel Pawlowski
22.3k 9 11 21

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Feb 08, 2012 at 08:50 PM

Seen: 1038 times

Last Updated: Feb 09, 2012 at 11:00 AM