65.9K
CodeProject is changing. Read more.
Home

SHRINKFILE and TRUNCATE Log File in SQL Server 2008

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Feb 23, 2011

CPOL
viewsIcon

31410

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