Transcript

SQL Server 2005 is quite different from SQL Server 2000. To truncate log file is one thing which is different from SQL Server 2000. In SQL Server 2000, you just use Shrink to whatever file size you like. In SQL Server 2005, sometimes I cannot shrink the log file at all.

Here I want to describe some tricks to truncate log file for a database in SQL Server 2005. The work environment is Microsoft SQL Server Management Studio.

I. Shrink the Log File Size at the Right Time

I found out this trick:

Immediately after I use the SSIS package or Import the data to the database (highlight the database->Tasks->Import data …), or Export the data from the database (highlight the database->Tasks->Export data …), I can shrink the log file to the desired size, for example, 1MB. That is, highlight the database->Tasks->Shrink->Files , set the file size, say, 1MB.

Then, click OK and you are done.

II. Eliminate the Log File Completely

Sometimes, we just do not need the big log file. For example, I have a 40GB log file. I am sure I do not need this log file and want to get rid of it completely to free up the hard drive space. The logic is:

1. Detach the database 2. Rename the log file 3. Attach the database without the log file 4. Delete the log file

Let’s say, the database name is testDev. In the SQL Server Management Studio,

1. Highlight the database-> Tasks->Detach..-> Click OK 2. Go to log file folder -> rename the testDev_log.ldf to be like testDev_log-aa.ldf3. Highlight Databases->Attach…-> Click Add -> add the database testDev, highlight the log file and click the ‘Remove’

button. This means you only attach testDev.mdf 4. After this is done, you can verify the contents of the attached database and then delete the log file

MSFT

17,895

Recent Achievements 15 8 1

Thread Mover II Proposed Answerer I Blog Party Starter

Balmukund's threads View Profile Microsoft(MSFT) 17,895

  

1

Sign In to Vote

Another method without detaching:

1. Run ALTER DATABASE command and change location of ldf file

2. Take database offline

3. Move the physical file from old location to new location.

3. Bring database online

Balmukund Lakhani | Please mark solved if I've answered your question, vote for it as helpful to help other user's

find a solution quicker

--------------------------------------------------------------------------------

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------------------------------------------------------------------

My Blog: http://blogs.msdn.com/blakhani

Team Blog: http://blogs.msdn.com/sqlserverfaq

Delete, Shrink, Eliminate Transaction Log .LDF FilePosted on April 1, 2011 by hoanhuynh

inShare0

In some case, the Microsoft SQL Server Database Transaction Log (.LDF) file becomes very big even ton of Gigabytes. It’s

wasting lot of disk space and causing some problems if you want to backup and restore the database.

To face with this issue, in this tutorial, you can apply either of following methods. I’m on Microsoft SQL Server 2008 but it also

be applied for Microsoft SQL Server 2005.

1. Shrink the transaction log (.ldf) file

By using this method, you can shrink the log to the desired size, ex: 1MB

+ Right-click on the database you want to shrink => Tasks => Shrink => Files

Microsoft SQL Server 2008 Shrink

+ On Shrink File box, select Log on File type drop down list and select Reorganize pages before releasing unused space then

set size for the log file, ex: 1 MB as picture below:

Microsoft SQL Server 2008 Shrink Set Size Log File

+ Click OK and you’re done.

The good point of this method is you can shrink at the right time but sometime, it doesn’t work especially with Microsoft SQL

Server 2005. Let’s try the second method below in case it’s does not work.

2. Completely delete the log file

The main idea of this method is it will delete the big log file and create a new log file with the minimum size.

Follow steps below:

1. Detach the database

Right-click on the database => Tasks => Detach…

Microsoft SQL Server 2008 Detach

2. Delete the big log file

This step will completely erase it from your hard drive. However, from my suggestion you should back up the log file somewhere,

ex: rename it from the file location (my case is: C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\

DATA)

Microsoft SQL Server 2008 Data Folder

3. Attach the database again

As we deleted the log file, so we do attach the database without the log file. Don’t worry, SQL Server will create a new log file for

you which will be of the minimum size. That’s perfect!

+ Right-click Databases => Attach…

Microsoft SQL Server 2008 Attach

+ On Attach Database box, click Add…

Microsoft SQL Server 2008 Attach Add

+ Browser to the database (.mdf) file then click OK

Microsoft SQL Server 2008 Browser MDF

+ Select the log (.ldf) file then click Remove

Microsoft SQL Server 2008 Attach Remove Log

Read more: http://4rapiddev.com/sql-server/delete-shrink-eliminate-transaction-log-ldf-file/#ixzz1Ybp7Nyw8


Recommended