• here's something i recently built to move specific clustered indexes to a new filegroup, and also enable page compression on some specific tables;

    you'll see some commented artifacts referencing a table variable @ItemsToMigrate.

    in my case, for a handful of specific tables, i created filegroups with a naming convention of db_name() + '_' + [object_name], ie "Production_CommandLog"

    you can simply modify that one line to be a hardcoded name of your other group.

    sample results

    GO

    CREATE UNIQUE CLUSTERED INDEX [PK_CommandLog]

    ON [dbo].[CommandLog] (ID asc)

    WITH (DROP_EXISTING = ON , DATA_COMPRESSION = PAGE)

    ON Production_CommandLog;

    the code:

    DECLARE @Results TABLE (

    [schema_id] INT,

    [schema_name] VARCHAR(255),

    [object_id] INT,

    [object_name] VARCHAR(255),

    [index_id] INT,

    [index_name] VARCHAR(255),

    [Rows] INT,

    [SizeMB] DECIMAL(19,3),

    [IndexDepth] INT,

    [type] INT,

    [type_desc] VARCHAR(30),

    [fill_factor] INT,

    [is_unique] INT,

    [is_primary_key] INT ,

    [is_unique_constraint] INT,

    [index_columns_key] VARCHAR(max),

    [index_columns_include] VARCHAR(max),

    [index_where_statement] VARCHAR(max),

    [currentFilegroupName] varchar(128),

    [CurrentCompression] varchar(128) )

    INSERT INTO @Results

    SELECT

    SCH.schema_id, SCH.[name] AS schema_name,

    OBJS.[object_id], OBJS.[name] AS object_name,

    IDX.index_id, ISNULL(IDX.[name], '---') AS index_name,

    partitions.Rows, partitions.SizeMB, IndexProperty(OBJS.[object_id], IDX.[name], 'IndexDepth') AS IndexDepth,

    IDX.type, IDX.type_desc, IDX.fill_factor,

    IDX.is_unique, IDX.is_primary_key, IDX.is_unique_constraint,

    ISNULL(Index_Columns.index_columns_key, '---') AS index_columns_key,

    ISNULL(Index_Columns.index_columns_include, '---') AS index_columns_include,

    ISNULL(' WHERE (' + IDX.filter_definition + ')','') AS index_where_statement,

    filz.name,

    ISNULL(p.data_compression_desc,'')

    FROM sys.objects OBJS

    INNER JOIN sys.schemas SCH ON OBJS.schema_id=SCH.schema_id

    -- INNER JOIN @ItemsToMigrate itm ON SCH.[name] = itm.SchemaName AND OBJS.[name] = itm.TableName

    INNER JOIN sys.indexes IDX ON OBJS.[object_id]=IDX.[object_id]

    INNER JOIN sys.filegroups filz ON idx.data_space_id = filz.data_space_id

    INNER JOIN sys.partitions p ON IDX.object_id = p.object_id AND IDX.index_id = p.index_id

    INNER JOIN (

    SELECT

    [object_id], index_id, SUM(row_count) AS Rows,

    CONVERT(numeric(19,3), CONVERT(numeric(19,3), SUM(in_row_reserved_page_count+lob_reserved_page_count+row_overflow_reserved_page_count))/CONVERT(numeric(19,3), 128)) AS SizeMB

    FROM sys.dm_db_partition_stats STATS

    GROUP BY [object_id], index_id

    ) AS partitions

    ON IDX.[object_id]=partitions.[object_id]

    AND IDX.index_id=partitions.index_id

    CROSS APPLY (

    SELECT

    LEFT(index_columns_key, LEN(index_columns_key)-1) AS index_columns_key,

    LEFT(index_columns_include, LEN(index_columns_include)-1) AS index_columns_include,

    LEFT(index_columns_where, LEN(index_columns_where)-1) AS index_columns_where

    FROM

    (

    SELECT

    (

    SELECT COLS.[name] + case when IXCOLS.is_descending_key = 0 then ' asc' else ' desc' end + ',' + ' '

    FROM sys.index_columns IXCOLS

    INNER JOIN sys.columns COLS

    ON IXCOLS.column_id = COLS.column_id

    AND IXCOLS.[object_id] = COLS.[object_id]

    WHERE IXCOLS.is_included_column = 0

    AND IDX.[object_id] = IXCOLS.[object_id]

    AND IDX.index_id = IXCOLS.index_id

    ORDER BY key_ordinal

    FOR XML PATH('')

    ) AS index_columns_key,

    (

    SELECT COLS.[name] + ',' + ' '

    FROM sys.index_columns IXCOLS

    INNER JOIN sys.columns COLS

    ON IXCOLS.column_id = COLS.column_id

    AND IXCOLS.[object_id] = COLS.[object_id]

    WHERE IXCOLS.is_included_column = 1

    AND IDX.[object_id] = IXCOLS.[object_id]

    AND IDX.index_id = IXCOLS.index_id

    ORDER BY index_column_id

    FOR XML PATH('')

    ) AS index_columns_include,

    (SELECT COLS.[name] + ',' + ' '

    FROM sys.index_columns IXCOLS

    INNER JOIN sys.columns COLS

    ON IXCOLS.column_id = COLS.column_id

    AND IXCOLS.[object_id] = COLS.[object_id]

    WHERE IXCOLS.is_included_column = 1

    AND IDX.[object_id] = IXCOLS.[object_id]

    AND IDX.index_id = IXCOLS.index_id

    ORDER BY index_column_id

    FOR XML PATH('')

    ) AS index_columns_where

    ) AS Index_Columns

    ) AS Index_Columns

    WHERE filz.name = 'PRIMARY'

    ORDER BY

    SCH.[name],

    OBJS.[name],

    IDX.[name];

    DECLARE @ForceCompression int = 1;

    SELECT [schema_name],[object_name], 'GO

    CREATE '+ CASE WHEN [is_unique]=1 THEN 'UNIQUE ' ELSE '' END + ' CLUSTERED INDEX ' + quotename(index_name) + '

    ON ' + quotename([schema_name]) + '.' + quotename([object_name]) + ' (' + index_columns_key + ') ' + index_where_statement + '

    WITH (DROP_EXISTING = ON ' + CASE

    WHEN @ForceCompression = 1

    THEN ', DATA_COMPRESSION = PAGE'

    WHEN [CurrentCompression] <> 'NONE'

    THEN ''

    ELSE ', DATA_COMPRESSION = ' + [CurrentCompression]

    END + ')

    ON ' + db_name() + '_' + [object_name] + ';'

    FROM

    @Results WHERE 1=1

    --AND [object_name] IN(SELECT TableName FROM @ItemsToMigrate)

    AND index_id = 1

    GO

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!