I have a mssql database, we'll call it: mssqlDB01. I've been tasked with performing defragmentation on all of the tables. This database has a few hundred tables and each table has a range of 1 to 15 indexes per table.
Google led me to discover a practice to defrag all indexes per table, but I can't figure out how to do it on all tables.
ALTER INDEX ALL ON TABLENAME REBUILD;
what i'm looking for is
ALTER INDEX ALL ON * REBUILD;
but it complains
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '*'.`
below lets me find all tables in my DB
SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE'
can I somehow push this into the command?
ALTER INDEX ALL ON (SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE'; ) REBUILD;
Best Answer
You could probably write a script that uses dynamic SQL to do that, but why do that when you can use someone else's? Ola Hallengren's are the best known and free, but Minion Ware also has a free reindex script.
If you insist on writing it yourself, something like this might work: