x

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

    avatar 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, 2012 at 09:29 PM

    avatar image

    SirSQL
    4.9k 4 5

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

    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

    avatar image

    Pavel Pawlowski
    22.7k 10 15 26

    (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

    SQL Server Central

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

    Topics:

    x42
    x13

    asked: Feb 08, 2012 at 08:50 PM

    Seen: 1234 times

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

    Copyright 2016 Redgate Software. Privacy Policy