简体   繁体   English

目标表分区为空,但更改表切换仍然失败,说明目标分区必须为空

[英]Destination table partition is empty but alter table switch still fails stating destination partition must be empty

I am working with SQL Server 2008. I have two 100 partitioned tables of same structure, partition schema/function and same file group. 我正在使用SQL Server2008。我有两个具有相同结构,分区架构/功能和相同文件组的100个分区表。

Table structure: 表结构:

Create table source_table
(
     id int, 
     XmlData xml, 
     Partitionkey ([id]%(100)) As Persisted
)

Create table Destination_table
(
     id int, 
     XmlData xml, 
     Partitionkey ([id]%(100)) As Persisted
)

Requirement: 需求:

Destination_table has records but partition 23 is empty. Destination_table有记录,但分区23为空。 I need to move partition 23 records from Source_table to Destination_table . 我需要将分区23记录从Source_table移到Destination_table

ALTER TABLE Source_table 
SWITCH partition 23 TO Destination_table partition 23

I get an error 我得到一个错误

ALTER TABLE SWITCH failed. ALTER TABLE SWITCH失败。 The target partition 23 of Destination_table must be empty. Destination_table的目标分区23必须为空。

The partition 23 of destination_table is already empty. destination_table的分区23已经为空。

Select count(1) 
from destination_table 

returns 0. 返回0。

Then why do I get this error? 那为什么我会得到这个错误?

Had a confusion between partition value and partition Id. 分区值和分区ID之间有混淆。 The partition value is 23 but the partition Id was 24 as the partition value started from 0 to 99 and the partition Id was 1 to 100. 分区值为23,但分区ID为24,因为分区值从0到99开始,分区ID为1到100。

So, the partition Id 24 worked for moving partition with value 23: 因此,分区ID 24用于移动值为23的分区:

ALTER TABLE Source_table SWITCH partition 24 TO Destination_table partition 24 ALTER TABLE Source_table切换分区24到Destination_table分区24

This structure would work 这种结构会起作用

Create table source_table
(
     id int, 
     XmlData xml, 
     Partitionkey ((([id] - 1) % (100)) + 1) As Persisted
)

Create table Destination_table
(
     id int, 
     XmlData xml, 
     Partitionkey ((([id] - 1) % (100)) + 1) As Persisted
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM