Rebuild All Database Indexes

Rebuild all the indexes within a Database

Script:

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 
        print 'DBCC DBREINDEX('+@TableName+')'
          DBCC DBREINDEX(@TableName) WITH NO_INFOMSGS
        FETCH NEXT FROM TableCursor INTO @TableName 
      END

CLOSE TableCursor
DEALLOCATE TableCursor
SET NOCOUNT OFF

Leave a Reply

Your email address will not be published. Required fields are marked *