Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

SHRINKFILE and TRUNCATE Log File in SQL Server 2008

5.00/5 (1 vote)
23 Feb 2011CPOL 31.4K  
SHRINKFILE and TRUNCATE Log File in SQL Server 2008
When we create a new database inside the SQL Server, it is typical that SQL Server creates two physical files in the Operating System: one with .MDF Extension, and another with .LDF Extension.
* .MDF is called as Primary Data File.
* .LDF is called as Transactional Log file.

Sometimes, it looks impossible to shrink the Truncated Log file. The following code always shrinks the Truncated Log File to minimum size possible.
SQL
USE DatabaseName
GO
ALTER DATABASE [DBName] SET RECOVERY SIMPLE WITH NO_WAIT
DBCC SHRINKFILE([DBName_log], 1)
ALTER DATABASE [DBName] SET RECOVERY FULL WITH NO_WAIT
GO

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)