【问题标题】:Destination table partition is empty but alter table switch still fails stating destination partition must be empty目标表分区为空,但更改表切换仍然失败,说明目标分区必须为空
【发布时间】:2016-02-06 13:48:59
【问题描述】:

我正在使用 SQL Server 2008。我有两个具有相同结构、分区架构/功能和相同文件组的 100 个分区表。

表结构:

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
)

要求:

Destination_table 有记录,但分区 23 为空。我需要将分区 23 记录从 Source_table 移动到 Destination_table

ALTER TABLE Source_table 
SWITCH partition 23 TO Destination_table partition 23

我收到一个错误

ALTER TABLE SWITCH 失败。 Destination_table 的目标分区 23 必须为空。

destination_table 的分区 23 已经为空。

Select count(1) 
from destination_table 

返回 0。

那为什么会出现这个错误呢?

【问题讨论】:

    标签: sql-server database sql-server-2008


    【解决方案1】:

    在分区值和分区 ID 之间存在混淆。分区值为 23,但分区 ID 为 24,因为分区值从 0 到 99,分区 ID 为 1 到 100。

    因此,分区 ID 24 用于移动值为 23 的分区:

    ALTER TABLE Source_table SWITCH 分区 24 TO Destination_table 分区 24

    【讨论】:

      【解决方案2】:

      这个结构可以工作

      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
      )
      

      【讨论】:

        猜你喜欢
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多