• Here is a query that generates the ALTER INDEX commands for you. Making this into a cursor is left as an exercise to the reader.

    Note: I don't have multi-filegroup database to test on, so you need to test carefully! I've tried to take in regard that that the tables may be partitioned

    SELECT 'ALTER INDEX ' + quotename(i.name) + ' ON ' +

    quotename(s.name) + '.' + quotename(o.name) +

    ' REORGANIZE PARTITION = ' + ltrim(str(p.partition_number))

    FROM sys.partitions p

    JOIN sys.indexes i ON p.object_id = i.object_id

    AND p.index_id = i.index_id

    JOIN sys.objects o ON o.object_id = p.object_id

    JOIN sys.schemas s ON o.schema_id = s.schema_id

    JOIN sys.allocation_units au ON p.hobt_id = au.container_id

    JOIN sys.data_spaces ds ON au.data_space_id = ds.data_space_id

    WHERE ds.name = 'PRIMARY'

    AND o.type = 'U'

    AND i.index_id > 0

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]