SQL Re-indexing/defrag

I took the code from this post by Tara Duggan which does either a DBCC REINDEX or a DBCC INDEXDEFRAG, depending on the parameters. The original intent of this was as a work around to the maintenance plan ‘Optimixations’ which seem to wrap all index rebuilding in a great big transaction. For large databases the mainenance plan would error if the log could not keep up with the plan, and log backups were not possible.

Code below; formatting is kind of off.

Update:

Having all that code in the post was pretty annoying. Link to the code here