Monday 24 July 2017

How to disable Data Compression on tables in whole database in SQL Server 2008R2?

If you restore database from enterprise edition to Standard edition and you have enabled "Data compression" on table, restoration fails. Data compression is available only Enterprise edition in SQL Server 2008R2. The best way how to disable data compression is the following:

1) make a clone of your database where data compression is enabled on enterprise edition

2) then run this script on clone database
SELECT
SCHEMA_NAME(sys.objects.schema_id) AS [SchemaName]
,OBJECT_NAME(sys.objects.object_id) AS [ObjectName]
,[rows]
,[data_compression_desc]
,[index_id] as [IndexID_on_Table]
into #compr
FROM sys.partitions
INNER JOIN sys.objects
ON sys.partitions.object_id = sys.objects.object_id 
WHERE data_compression > 0
AND SCHEMA_NAME(sys.objects.schema_id) <> 'SYS'
while exists(select 1 from #compr)
begin
      declare @i varchar(200) = (select top 1 [ObjectName] from #compr)
      print @i
     exec ('ALTER INDEX ALL ON '+@i + ' REBUILD WITH (DATA_COMPRESSION = None)')
     delete from #compr where [ObjectName] = @i
end
drop table #compr

3) now you can backup clone database and restore on Standard edition