SELECT 'ALTER INDEX ' + x.name + ' ON ' + s.name + '.' + o.name 
       + ' REBUILD WITH (FILLFACTOR = 5);  -- ' + Cast(Round(p.avg_fragmentation_in_percent, 1) AS varchar(6))
  FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED') p
       INNER JOIN sys.indexes x ON x.object_id = p.object_id and x.index_id = p.index_id
       INNER JOIN sys.objects o ON o.object_id = p.object_id
       INNER JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE p.avg_fragmentation_in_percent > 10
ORDER BY p.avg_fragmentation_in_percent DESC, o.name