Run DBCC and store results into table

Easy way to run DBCC command and to save results into table for further analysis:

use dba
GO

if not exists (select null from sysobjects where name = ‘DBA_CheckShowContig_Results’)
CREATE TABLE DBA_CheckShowContig_Results (
ObjectName VARCHAR (255),
ObjectId INT,
IndexName VARCHAR (255),
IndexId INT,
Lvl INT,
CountPages INT,
CountRows INT,
MinRecSize INT,
MaxRecSize INT,
AvgRecSize INT,
ForRecCount INT,
Extents INT,
ExtentSwitches INT,
AvgFreeBytes INT,
AvgPageDensity INT,
ScanDensity DECIMAL,
BestCount INT,
ActualCount INT,
LogicalFrag DECIMAL,
ExtentFrag DECIMAL)
ELSE
truncate table DBA_CheckShowContig_Results

GO

declare @dbname nvarchar(100)
declare @cmd nvarchar(2000)

set @dbname = ‘ChemSpider’

set @cmd = ‘use ‘ + @dbname + ‘; exec sp_MSForEachTable @command1 = ”insert into DBA.dbo.DBA_CheckShowContig_Results exec(””dbcc showcontig([?]) with tableresults, all_indexes””)”’
exec (@cmd)

–select * from DBA_CheckShowContig_Results

Update me when site is updated

Leave a Reply

You must be logged in to post a comment.