简体   繁体   中英

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.

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. I need to move partition 23 records from Source_table to Destination_table .

ALTER TABLE Source_table 
SWITCH partition 23 TO Destination_table partition 23

I get an error

ALTER TABLE SWITCH failed. The target partition 23 of Destination_table must be empty.

The partition 23 of destination_table is already empty.

Select count(1) 
from destination_table 

returns 0.

Then why do I get this error?

Had a confusion between partition value and partition 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.

So, the partition Id 24 worked for moving partition with value 23:

ALTER TABLE Source_table SWITCH partition 24 TO Destination_table partition 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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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