IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[#CheckDBResult]')) DROP TABLE #CheckDBResult
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[#CheckDBResult2005]')) DROP TABLE #CheckDBResult2005
IF SUBSTRING(@@version,23,4)='2000'BEGIN

CREATE TABLE
dbo.#CheckDBResult(ServerName VARCHAR(100) NULL,Error INT NULL,LEVEL INT NULL,State INT NULL,MessageText VARCHAR(7000) NULL,RepairLevel INT NULL,Status INT NULL,DbId INT NULL,Id INT NULL,IndId INT NULL,[File] INT NULL,Page INT NULL,Slot INT NULL,RefFile INT NULL,RefPage INT NULL,RefSlot INT NULL,Allocation INT NULL,insert_date DATETIME NOT NULL CONSTRAINT DF_Check_DBResult_insert_date  DEFAULT (GETDATE())
)

INSERT INTO dbo.#CheckDBResult(
  
Error,
  
LEVEL,
  
State,
  
MessageText,
  
RepairLevel,
  
Status,
  
DbId,
  
Id,
  
IndId,
  
[File],
  
Page,
  
Slot,
  
RefFile,
  
RefPage,
  
RefSlot,
  
Allocation
  
)EXEC sp_MSFOREACHDB'DBCC CHECKDB ( ''?'') WITH TABLERESULTS';
-- Select Statment To Return Rows
SELECT S.name AS [Database],T.MessageText,T.Error,T.LEVEL,T.State,T.RepairLevel,T.Status,T.DbId,T.Id,T.IndId,T.[File],T.Page,T.Slot,T.RefFile,T.RefPage,T.RefSlot,T.Allocation,T.Insert_DateFROM dbo.#CheckDBResult TINNER JOIN sysdatabases S ON T.DbId = S.database_id


END
ELSE
BEGIN
CREATE TABLE
dbo.#CheckDBResult2005(ServerName VARCHAR(100) NULL,Error INT NULL,LEVEL INT NULL,State INT NULL,MessageText VARCHAR(7000) NULL,RepairLevel INT NULL,Status INT NULL,DbId INT NULL,Id INT NULL,IndId INT NULL,PartitionId INT NULL,AllocUnitId INT NULL,[File] INT NULL,Page INT NULL,Slot INT NULL,RefFile INT NULL,RefPage INT NULL,RefSlot INT NULL,Allocation INT NULL,insert_date DATETIME NOT NULL CONSTRAINT DF_Check_DBResult05_insert_date  DEFAULT (GETDATE())
)


INSERT INTO dbo.#CheckDBResult2005(
  
Error,
  
LEVEL,
  
State,
  
MessageText,
  
RepairLevel,
  
Status,
  
DbId,
  
Id,
  
IndId,
  
PartitionId,             -- specific to SQL Server 2005, removed for SQL Server 2000 results
  
AllocUnitId,             -- specific to SQL Server 2005, removed for SQL Server 2000 results
  
[File],
  
Page,
  
Slot,
  
RefFile,
  
RefPage,
  
RefSlot,
  
Allocation
  
)
EXEC sp_MSFOREACHDB'DBCC CHECKDB ( ''?'') WITH TABLERESULTS';
-- Select Statment To Return Rows
SELECT S.name AS [Database],T.MessageText,T.Error,T.LEVEL,T.State,T.RepairLevel,T.Status,T.DbId,T.Id,T.IndId,T.PartitionId,          -- specific to SQL Server 2005, removed for SQL Server 2000 resultsT.AllocUnitId,          -- specific to SQL Server 2005, removed for SQL Server 2000 resultsT.[File],T.Page,T.Slot,T.RefFile,T.RefPage,T.RefSlot,T.Allocation,T.Insert_DateFROM dbo.#CheckDBResult2005 TINNER JOIN sys.databases S ON T.DbId = S.database_id
END

IF
EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[#CheckDBResult]')) DROP TABLE #CheckDBResult
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id = OBJECT_ID(N'[tempdb]..[#CheckDBResult2005]')) DROP TABLE #CheckDBResult2005