首页 > 代码库 > SQL SERVER出现大量一致性错误的解决方法

SQL SERVER出现大量一致性错误的解决方法

  如果DBCC CHECKDB发现了比较少的一致性错误,可以使用   DBCC UPDATEUSAGE(DatabaseName,"dbo.ObjectName"); 语句逐个针对表或索引中的每个分区更正行、已用页、保留页、叶级页和数据页的计数。但是如果数据库出现大量一致性错误。例如如下所示:

DBCC results for JO_T_CUTXX.

消息 2508,级别 16,状态 1,第 1 行

The In-row data USED page count for object "JO_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 "JO_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 "JO_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 "JO_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 "JO_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 "JO_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 "JO_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 "JO_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 "JO_T_CUTXX".

CHECKDB found 0 allocation errors and 8 consistency errors in table JO_T_CUTXX (object ID 972634608).

....................................................................................

....................................................................................

....................................................................................
View Code

 

那么逐个语句去将目录视图中的页数和行数错误进行更正就显得有点低效和枯燥。如果数据库不大,你想对全部表执行以下该语句,那么可以使用下面语句:(当然更新统计信息语句不是必须的,为了确保系统视图、表信息正确性,有必要先收集、更新一下统计信息)

Code Snippet
  1. USE master
  2.  
  3. GO
  4.  
  5. EXEC sp_MSforeachdb@command1="print ‘?‘ EXEC [?].dbo.sp_updatestats"
  6.  
  7. USE master
  8.  
  9. GO
  10.  
  11. EXEC sp_MSforeachdb@command1="print ‘?‘ DBCC UPDATEUSAGE (?)"

缺点是会对所有对象执行一次DBCC UPDATEUSAGE,如果数据库比较大,这个脚本执行起来就比较慢了。