Below you will find the code for the Index Fragmentation the database. Simply select the text and copy to the Clipboard.
SELECT OBJECT_NAME(object_id) AS ‘Table_Name’,
WHERE (object_id = i.object_id) AND (index_id = i.index_id)) AS ‘Index_Name’, avg_fragmentation_in_percent, page_count, index_type_desc
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) AS ‘i’
WHERE avg_fragmentation_in_percent > 20
ORDER BY avg_fragmentation_in_percent DESC
This code was written using the Microsoft SQL Server documentation. If you find that this code was copied from else where, please let me know so I can give proper credit.