Ok I put my first defrag script out last March and it had a few issues. First and foremost it had a few bugs this one is bug free. It does have one exception , it should not be used on tables that are partitioned that have non clustered indexes that are not partitioned on the same partition as the table. This is not a normal occurrence so I don’t feel bad about putting this script out there without this functionality. This version will execute sp_update_stats with the @resample set to “RESAMPLE” as to not change any sampling rates. With the last post I included the script as part of the post with HTML formatting. I found this didn’t copy that well for me so I have included this time as plain text.
If you have an questions or comments please let me know also if you find any bugs I would like to know so I can correct them.
Update: This script also does not deal with tables that are set to row level locking only, and it does not rebuild heap tables.