[SQL]MS SQL 清除 Transaction Log Script

MS SQL 清除 Transaction Log Script

分享我們家Wilson整理的MS清Transaction Log的Script,因為SQL的每個版本都不同,所以在此記錄一下。

SQL 7.0/2000:

BACKUP TRANSACTION DBName WITH TRUNCATE_ONLY DBCC SHRINKFILE(2,200)

SQL 2005:

在 SQL 2005 中備份 Transaction Log 語法改為 Backup LOG

BACKUP LOG DBName WITH NO_LOG

DBCC SHRINKFILE(2,200)

SQL 2008:

必須先將復原模式改為 "簡單" 才能清除,完成之後再將模式改回 "完整"

USE DBName

Alter Database DBName Set Recovery Simple DBCC SHRINKFILE(2,100) Alter Database DBName Set Recovery Full

SQL 2008 R2:

USE DBName;

GO

-- Truncate the log by changing the database recovery model to SIMPLE.

ALTER DATABASE DBName

SET RECOVERY SIMPLE;

GO

-- Shrink the truncated log file to 10 MB.

DBCC SHRINKFILE (2, 10);

GO

-- Reset the database recovery model.

ALTER DATABASE DBName

SET RECOVERY FULL;

GO

*上述 DBCC  SHRINKFILE (2, 10),2指的是LOG File, 10指的是壓到10MB哦!

參考資料:

DBCC SHRINKFILE (Transact-SQL)

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^