rebuild indexes for all tables

Feb 24 2011 Published by raja under sql

one of the SQL scripts i always look for is to rebuild indexes, across all tables.

  • there seemed like no simple SQL script to rebuild them in one shot.
  • it was always looping through the tables and running execute sql.

till i stumbled onto this. an undocumented SP to perform dynamic sql, for every table. the keyword being undocumented.. meaning, discretion, disclaimer :)

sp_MSforeachtable @SqlToExecute=”print ‘?’ DBCC DBREINDEX (‘?’)”

as you can see, ‘?’ stands for the table name.

No responses yet