kodaksmile
MIS
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
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