在 SQL Server 中,当通过数据库备份和还原,导入和导出等操作时,表的压缩设置可能会丢失。因此,需要在这些操作之前先保存表的压缩设置,以便在还原或导入时重新应用这些设置。
示例代码:
-- 保存表的压缩设置 SELECT name, is_compressed INTO dbo.TableCompressionSettings FROM sys.partitions WHERE OBJECTPROPERTYEX(OBJECT_ID, 'IsUserTable') = 1
-- 恢复表的压缩设置 DECLARE @TableName NVARCHAR(MAX) DECLARE @CompressionSetting BIT
DECLARE CompressionSettingsCursor CURSOR FOR SELECT name, is_compressed FROM dbo.TableCompressionSettings
OPEN CompressionSettingsCursor
FETCH NEXT FROM CompressionSettingsCursor INTO @TableName, @CompressionSetting
WHILE @@FETCH_STATUS = 0 BEGIN IF @CompressionSetting = 1 EXEC sp_tableoption @TableName, 'compression', 'row'; ELSE IF @CompressionSetting = 0 EXEC sp_tableoption @TableName, 'compression', 'none';
FETCH NEXT FROM CompressionSettingsCursor
INTO @TableName, @CompressionSetting
END
CLOSE CompressionSettingsCursor DEALLOCATE CompressionSettingsCursor
-- 删除保存的表压缩设置 DROP TABLE dbo.TableCompressionSettings
上一篇:避免丢失片段实例
下一篇:避免div元素重叠对齐