x
login about faq Site discussion (meta-askssc)

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 '12 at 08:50 PM in Default

subramanyam gravatar image

subramanyam
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

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.

more ▼

answered Feb 08 '12 at 09:29 PM

SirSQL gravatar image

SirSQL
4.6k 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 '12 at 11:00 AM

Pavel Pawlowski gravatar image

Pavel Pawlowski
20.3k 5 10 20

(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.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x33
x4

asked: Feb 08 '12 at 08:50 PM

Seen: 531 times

Last Updated: Feb 09 '12 at 11:00 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.