Archive

Archive for the ‘Rebuild Database Indexes’ Category

Rebuild Database Indexes

November 11, 2014 Leave a comment

The below script will rebuild all indexes with in a database

DECLARE @TableName varchar(255)
DECLARE TableCursor CURSOR FOR
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_type = ‘BASE TABLE’ and table_name not in (‘sysdiagrams’)

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
DBCC DBREINDEX(@TableName)
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor