select
s.name as schema_name
,t.name as table_name
,i.name as index_name
,c.avg_fragmentation_in_percent
from sys.dm_db_index_physical_stats(db_id(), null, null, null, 'LIMITED') as c
inner join sys.tables as t
on c.object_id = t.object_id
inner join sys.schemas as s
on t.schema_id = s.schema_id
inner join sys.indexes as i
on c.index_id = i.index_id
where c.avg_fragmentation_in_percent > 5
order by c.avg_fragmentation_in_percent desc;
ALTER INDEX { index_name | ALL } ON <object> REBUILD
ALTER INDEX { index_name | ALL } ON <object> REORGANIZE