如果DBCC CHECKDB发现了比较少的一致性错误,可以使用 DBCC UPDATEUSAGE(DatabaseName,"dbo.TableName"); 语句逐个针对表或索引中的每个分区更正行、已用页、保留页、叶级页和数据页的计数。但是如果数据库出现大量一致性错误。例如如下所示:
DBCC results for 'T_CUTXX'.
消息 2508,级别 16,状态 1,第 1 行
The In-row data USED page count for object "T_CUTXX", index ID 2, partition ID 626692535091200, alloc unit ID 626692535091200 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 3,第 1 行
The In-row data RSVD page count for object "T_CUTXX", index ID 2, partition ID 626692535091200, alloc unit ID 626692535091200 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 1,第 1 行
The In-row data USED page count for object "T_CUTXX", index ID 3, partition ID 908167511801856, alloc unit ID 908167511801856 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 3,第 1 行
The In-row data RSVD page count for object "T_CUTXX", index ID 3, partition ID 908167511801856, alloc unit ID 908167511801856 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 1,第 1 行
The In-row data USED page count for object "T_CUTXX", index ID 4, partition ID 1189642488512512, alloc unit ID 1189642488512512 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 3,第 1 行
The In-row data RSVD page count for object "T_CUTXX", index ID 4, partition ID 1189642488512512, alloc unit ID 1189642488512512 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 1,第 1 行
The In-row data USED page count for object "T_CUTXX", index ID 5, partition ID 1471117465223168, alloc unit ID 1471117465223168 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
消息 2508,级别 16,状态 3,第 1 行
The In-row data RSVD page count for object "T_CUTXX", index ID 5, partition ID 1471117465223168, alloc unit ID 1471117465223168 (type In-row data) is incorrect. Run DBCC UPDATEUSAGE.
There are 0 rows in 0 pages for object "T_CUTXX".
CHECKDB found 0 allocation errors and 8 consistency errors in table 'T_CUTXX' (object ID 972634608).
.................................................................................................................
.................................................................................................................
那么逐个语句去报告目录视图中的页数和行数错误并进行更正就显得有点低效和枯燥。如果数据库不大,你想对全部表执行以下该语句,那么可以使用下面语句:
USE master
GO
EXEC sp_MSforeachdb @command1="print '?' EXEC [?].dbo.sp_updatestats"
USE master
GO
EXEC sp_MSforeachdb @command1="print '?' DBCC UPDATEUSAGE (?)"
缺点是会对所有对象执行一次DBCC UPDATEUSAGE,如果数据库比较大,这个脚本执行起来就比较慢了。
该贴被潇湘隐者编辑于2014-5-12 11:01:39该贴由hui.chen转至本版2014-11-5 16:24:20