• If you are developing an in-house index degfragmentation tool, I personally wouldn't invest the time reinventing the wheel. There are several options already developed and freely available on the web.

    We use this one in house, courtesy of Ola Hallengren.

    http://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html