SQL Server一次切换多个分区

时间:2013-07-12 02:00:27

标签: sql-server sql-server-2008 switch-statement database-partitioning

我在表中有100个parititons,我需要切换到相应的阶段表(在相同的FG和对齐的索引上) 目前我正在使用下面的查询

更改表MS_PROD     将分区5切换到MS_Stage PARTITION 5

我最终为所有100个分区执行此操作,是否有一种快速方法可以并行切换所有分区。

1 个答案:

答案 0 :(得分:2)

不是我知道的。我通常做的是将开关置于环路内。像这样:

DECLARE @Partitions TABLE (PartitionId int PRIMARY KEY CLUSTERED);
DECLARE @PartitionId INT;

INSERT @Partitions(PartitionId)
    SELECT 
        prv.boundary_id PartitionId
    FROM sys.partition_functions AS pf 
    INNER JOIN sys.partition_range_values prv ON prv.function_id=pf.function_id
    WHERE (pf.name=N'PartitionFunctionName');

WHILE EXISTS (SELECT NULL FROM @Partitions)
    BEGIN

        SELECT TOP 1 @PartitionId = PartitionId FROM @Partitions;

        ALTER TABLE MS_PROD SWITCH PARTITION @PartitionId TO MS_Stage PARTITION @PartitionId;

        RAISERROR('Switched PartitionId %d to Stage',0,1,@PartitionId) WITH NOWAIT;

        DELETE @Partitions WHERE PartitionId = @PartitionId;

    END