Script Reindex All Table in Database





DECLARE @MyTable VARCHAR(255)

DECLARE myCursor CURSOR FOR
 SELECT table_name
 FROM information_schema.tables
 WHERE table_type = 'base table'
 Order by table_name 

OPEN myCursor FETCH NEXT FROM myCursor INTO @MyTable

WHILE @@FETCH_STATUS = 0
BEGIN
 PRINT 'Reindexing Table:  ' + @MyTable + '...' + Convert(Char(20),Getdate(),114)
 DBCC DBREINDEX(@MyTable, '', 80)
 FETCH NEXT FROM myCursor INTO @MyTable
END

CLOSE myCursor
DEALLOCATE myCursor

EXEC sp_updatestats

Leave a Reply

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