This script utilizes the ALTER INDEX command to rebuild all indexes in a database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DECLARE @cmd VARCHAR(1000); DECLARE cIndex CURSOR FOR SELECT 'ALTER INDEX ALL ON ' + QUOTENAME(OBJECT_SCHEMA_NAME(OBJECT_ID)) + '.' + QUOTENAME(name) + ' REBUILD;' FROM sys.tables ORDER BY OBJECT_SCHEMA_NAME(object_id), name; OPEN cIndex FETCH NEXT FROM cIndex INTO @cmd; WHILE @@FETCH_STATUS = 0 BEGIN RAISERROR(@cmd, 10, 1) WITH NOWAIT; EXEC (@cmd); FETCH NEXT FROM cIndex INTO @cmd; END; CLOSE cIndex; DEALLOCATE cIndex; |