Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server一次切换多个分区_Sql Server_Sql Server 2008_Switch Statement_Database Partitioning - Fatal编程技术网

Sql server SQL Server一次切换多个分区

Sql server SQL Server一次切换多个分区,sql-server,sql-server-2008,switch-statement,database-partitioning,Sql Server,Sql Server 2008,Switch Statement,Database Partitioning,我在一个表中有100个配对,我需要切换到相应的阶段表(在同一个FG上,并具有对齐的索引) 目前我正在使用下面的查询 更改表MS_PROD 将分区5切换到MS_阶段分区5 我最后对所有100个分区都这样做了,有没有一种快速的方法可以并行切换所有分区。我不知道。我通常会把开关放在循环中。大概是这样的: DECLARE @Partitions TABLE (PartitionId int PRIMARY KEY CLUSTERED); DECLARE @PartitionId INT; INSERT

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

更改表MS_PROD 将分区5切换到MS_阶段分区5


我最后对所有100个分区都这样做了,有没有一种快速的方法可以并行切换所有分区。

我不知道。我通常会把开关放在循环中。大概是这样的:

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

不。您需要一个迭代过程(循环/游标)来循环并执行每个命令。这显然是一个串行操作。您还必须记住swtich是一个元数据操作,因此并行运行不会为您节省太多时间。