• Well, you're not really using much of your query ... that and why are you changing your recovery models for this operation? The ALTER index does not bloat the log files like DBCC DBREINDEX/DEFRAG used to.

    You're also not taking into account partitions, the ability to reorganize or rebuild, as well as online operations. I'd recommend using the example in BOL and customizing it to your environment's needs.

    Anyways, here is a condensed version of yours ... :

    This should give you all your indexes ...

    SELECT

    a.index_id

    ,b.name

    ,a.avg_fragmentation_in_percent

    ,db_name(db_id()) AS [DBName]

    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL,NULL, NULL, NULL) AS a

    JOIN sys.indexes AS b

    ON a.object_id = b.object_id

    AND a.index_id = b.index_id

    WHERE a.index_id > 0

    AND a.index_id < 255