日期:2014-05-16  浏览次数:20609 次

保存dbcc checkdb 的结果
保存dbcc checkdb 的结果
循环执行多个数据
while(@<10)
begin
  dbcc checkdb ('数据') 把每个数据库的执行的结果存放在一个表中(要分清是哪个数据的结果)或文件中
end
更进一步:只把错误的结果保存
------解决方案--------------------
试试这个:

declare @i int
declare @c int
declare @v nvarchar(100)

declare @result table(
dbname nvarchar(100)
,Error INT
, Level INT
, State INT
, MessageText NVARCHAR (2048)
, RepairLevel NVARCHAR (22)
, Status INT
, DbId INT
, ObjectId INT
, IndexId INT
, PartitionId BIGINT
, AllocUnitId BIGINT
, [File] SMALLINT
, Page INT
, Slot INT
, RefFile SMALLINT
, RefPage INT
, RefSlot INT
, Allocation SMALLINT
)

declare @t table(id int identity(1,1),dbname nvarchar(100))

insert into @t
select Name from sysdatabases


set @i = 1
set @c = (select count(*) from @t)

while(@i<=@c)
begin
  select @v = dbname from @t where id = @i
  
  insert into @result(Error, Level, State, MessageText, RepairLevel, Status, DbId, ObjectId, IndexId, PartitionId, AllocUnitId, [File], Page, Slot, RefFile, RefPage, RefSlot, Allocation)
  exec('dbcc checkdb ('+@v+') with tableresults')
  
  update @result
  set dbname = @v
  where dbname is null
  
  set @i = @i + 1 
end


--查询dbcc checkdb的结果
select * from @result

------解决方案--------------------
这个是查询: