User Tools

Site Tools


notes:sql:reindexing

Reindexing SQL Tables

The following script reindexes all tables in the current database:

DECLARE @TABLE VARCHAR(255)
 
DECLARE cur CURSOR FOR 
    SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE='BASE TABLE'
 
OPEN cur
 
FETCH NEXT FROM cur INTO @TABLE
 
WHILE @@FETCH_STATUS=0
BEGIN
  PRINT 'Reindexing ' + @TABLE
  DBCC DBREINDEX(@TABLE)
  FETCH NEXT FROM cur INTO @TABLE
END
 
CLOSE cur
DEALLOCATE cur
notes/sql/reindexing.txt · Last modified: 2015/06/24 by admin