-- tables EXEC sp_MSForEachTable 'ALTER TABLE ? REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE)' GO -- indexes EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE )' GO SELECT 'ALTER TABLE ' + QUOTENAME([s].[name]) + '.' + QUOTENAME([t].[name]) + ' REBUILD PARTITION = ALL WITH (DATA_COMPRESSION = PAGE)' FROM [sys].[tables] [t] INNER JOIN [sys].[schemas] [s] ON [s].[schema_id] = [t].[schema_id] WHERE [s].[name] = ? ORDER BY [s].[name], [t].[name]