SELECT 'alter index ' + i.name + ' on ' + OBJECT_NAME(i.id) + ' rebuild'
FROM Manufacture.sys.sysindexes i ,
Manufacture.sys.dm_db_index_physical_stats(DB_ID('希望查找的数据库名称'), NULL,
NULL, NULL, 'SAMPLED') o
WHERE i.id = o.object_id
AND i.indid = o.index_id
AND o.page_count > @索引页总数
AND avg_page_space_used_in_percent < @页满度
AND i.name NOT LIKE '_WA%'