2009年4月21日星期二

SQL Server 2005数据库置疑状态的修复

重起服务器的时候遭遇了数据库置疑状态,在网上查找帖子,发现都是针对SQL2000的,不过借助这个原理,转换了一下SQL语句,成功解决,使用的脚本如下:

这个脚本适应于日志损坏的情况,在SQL Log里有如下信息:
反映的是日志状态不对。

Recovery of database 'SuspectDB' (5) is 96% complete (approximately 0 seconds remain). Phase 3 of 3. This is an informational message only. No user action is required.
SQL Server detected a DTC/KTM in-doubt transaction with UOW {D5141B98-7BB7-4D42-BF0D-F294CB378AB7}.Please resolve it following the guideline for Troubleshooting DTC Transactions.
错误: 3437,严重性: 21,状态: 3。
An error occurred while recovering database 'SuspectDB'. Unable to connect to Microsoft Distributed Transaction Coordinator (MS DTC) to check the completion status of transaction (0:32748311). Fix MS DTC, and run recovery again.
错误: 3414,严重性: 21,状态: 2。
An error occurred during recovery, preventing the database 'SuspectDB' (database ID 5) from restarting. Diagnose the recovery errors and fix them, or restore from a known good backup. If errors are not corrected or expected, contact Technical Support.



alter database SuspectDB set emergency
go
alter database SuspectDB set single_user with rollback immediate
go
use master
go
alter database SuspectDB Rebuild Log on
(name=SuspectDB_log,filename='D:\Log\SuspectDB_log.LDF')
go
alter database SuspectDB set multi_user
go

DBCC CHECKDB('SuspectDB')
go