Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a dbcc showcontig on MSSQL7

Status
Not open for further replies.
Jun 19, 2002
294
US
Hi all,

I am using the code below to try to run the dbcc showcontig and have the results stored in a table so we can decide which indexes to rebuild. The problem is the code below does not insert the data into the table it only returns the output to the screen. This is MSSQL 7 so it needs the tableid not the table name (I have a script for my 2000 servers) Anyone have any ideas?


-- Declare variables
USE pubs
drop table fraglist
go
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr VARCHAR (255)
DECLARE @objectid INT
DECLARE @indexid INT
DECLARE @frag DECIMAL
DECLARE @maxfrag DECIMAL
DECLARE @sql VARCHAR(255)

-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 10.0

-- Declare cursor
DECLARE tables CURSOR FOR
SELECT OBJECT_ID(TABLE_NAME)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

-- Create the table
CREATE TABLE fraglist (
ObjectName CHAR (255),
ObjectId INT,
IndexName CHAR (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)

-- Open the cursor
OPEN tables

-- Loop through all the tables in the database
FETCH NEXT
FROM tables
INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN
-- Do the showcontig of all indexes of the table
INSERT INTO fraglist
EXEC ('DBCC SHOWCONTIG ( '+ @tablename + ')
WITH TABLERESULTS, NO_INFOMSGS')
FETCH NEXT
FROM tables
INTO @tablename
END

-- Close and deallocate the cursor
CLOSE tables
DEALLOCATE tables



Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top