Quantcast
Viewing all articles
Browse latest Browse all 30

T-SQL: Rebuild or Reorganize Indexes for All Tables in a Database

Using the handy-dandy hidden Microsoft goodie “sp_msForEachTable”, you can easily rebuild all indexes on all tables in a user database with a one line command. Here’s how:

-- To rebuild with the default fill-factor
EXECUTE sp_msForEachTable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD;'

-- Or change one keyword to reorganize the indexes
EXECUTE sp_msForEachTable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REORGANIZE;'

Hope this helps!


Viewing all articles
Browse latest Browse all 30

Trending Articles