Saturday, September 17, 2011

Generate T-SQL for SQL 2008 R2 Database Compression





The following two query are for Microsoft SQL, they will generate T-SQL needs to enable a current database objects (tables and indexs) to be alerted with database compression.

Use this query set for databases that don't have the objects compressed.




--Creates the ALTER TABLE Statements



SET NOCOUNT ON

SELECT 'ALTER TABLE ' + '[' + s.[name] + ']'+'.' + '[' + o.[name] + ']' + ' REBUILD WITH (DATA_COMPRESSION=PAGE);'

FROM sys.objects AS o WITH (NOLOCK)

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON o.[object_id] = i.[object_id]

INNER JOIN sys.schemas AS s WITH (NOLOCK)

ON o.[schema_id] = s.[schema_id]

INNER JOIN sys.dm_db_partition_stats
AS ps WITH (NOLOCK)

ON i.[object_id] = ps.[object_id]

AND ps.[index_id] = i.[index_id]

WHERE o.[type] = 'U'

ORDER BY ps.[reserved_page_count]



--Creates the ALTER INDEX Statements


SET NOCOUNT ON

SELECT 'ALTER INDEX '+ '[' + i.[name] + ']' + ' ON ' + '[' + s.[name] + ']' + '.' + '[' + o.[name] + ']' + ' REBUILD WITH (DATA_COMPRESSION=PAGE);'

FROM sys.objects AS o WITH (NOLOCK)

INNER JOIN sys.indexes AS i WITH (NOLOCK)

ON o.[object_id] = i.[object_id]

INNER JOIN sys.schemas s WITH (NOLOCK)

ON o.[schema_id] = s.[schema_id]

INNER JOIN sys.dm_db_partition_stats
AS ps WITH (NOLOCK)

ON i.[object_id] = ps.[object_id]

AND ps.[index_id] = i.[index_id]

WHERE o.type = 'U' AND i.[index_id] >0

ORDER BY ps.[reserved_page_count]



0 comments: