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;'
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!