SQL Server 2000


Rebuild all indexes with keeping the default fill factor for each index
USE [DATABASE_NAME]
EXEC sp_MSforeachtable @command1=”print ‘?’ DBCC DBREINDEX (‘?’)”


Rebuild all indexes with specifying the fill factor
USE [DATABASE_NAME]
EXEC sp_MSforeachtable @command1=”print ‘?’ DBCC DBREINDEX (‘?’, ‘ ‘, [FILL_FACTOR_PERC])”



SQL Server 2005/2008
You can either use the syntax provided above for SQL Server 2000 or:


Rebuild all indexes online with keeping the default fill factor for each index
USE [DATABASE_NAME]
EXEC sp_MSforeachtable @command1=”print ‘?'”, @command2=”ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=ON)”

Rebuild all indexes offline with keeping the default fill factor for each index
USE [DATABASE_NAME]
EXEC sp_MSforeachtable @command1=”print ‘?'”, @command2=”ALTER INDEX ALL ON ? REBUILD WITH (ONLINE=OFF)”


Rebuild all indexes online with specifying the fill factor
USE [DATABASE_NAME]
EXEC sp_MSforeachtable @command1=”print ‘?'”, @command2=”ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR=[FILL_FACTOR_PERC],ONLINE=ON)”


Rebuild all indexes offline with specifying the fill factor
USE [DATABASE_NAME]

EXEC sp_MSforeachtable @command1=”print ‘?'”, @command2=”ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR=[FILL_FACTOR_PERC],ONLINE=OFF)”



rebuild indexes in sql server