DECLARE @maxfrag    Int
SELECT @maxfrag = 5
SELECT object_id, index_id, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID('MaBase')
,OBJECT_ID('MaTable')
,NULL 
,NULL
,'DETAILED')
WHERE avg_fragmentation_in_percent > CONVERT(CHAR(2),@maxfrag) AND Index_id > 0
ORDER BY CONVERT(DECIMAL(9,2),avg_fragmentation_in_percent) DESC;
