There are four ways to use the ALTER TABLE SWITCH statement: Switch from a non- partitioned table to another non- partitioned table. Load data by switching in: Switch from a non- partitioned table to a partition in a partitioned table. Archive data by switching out: Switch from a partition in a partitioned table to a non- partitioned table. Switch from a partition in a partitioned table to a partition in another partitioned table.
The following examples use code from the previous Table Partitioning Basics blog post. It is important to notice that these examples are meant to demonstrate the different ways of switching partitions, they do not create any indexes and they map all partitions to the . These examples are not meant to be used in real- world projects. Switch from Non- Partitioned to Non- Partitioned. The first way to use the ALTER TABLE SWITCH statement is to switch all the data from a non- partitioned table to an empty non- partitioned table.
ALTER TABLE Source SWITCH TO Target. ALTERTABLESource. SWITCHTOTarget. Before switch: After switch: This is probably not used a lot, but it is a great way to start learning the ALTER TABLE SWITCH statement without having to create partition functions and partition schemes. Drop objects if they already exist. IF EXISTS (SELECT * FROM sys.
WHERE name = N'Sales. Source'). DROP TABLE Sales. Source. IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales. Target'). DROP TABLE Sales.
Target. - - Create the Non- Partitioned Source Table (Heap) on the ? The target table 'Sales. Target' must be empty. SUPER FAST!- - Turn off statistics. SETSTATISTICSTIMEOFF; -- Verify row count after switch.
SELECTCOUNT(*)FROMSales. Source; -- 0 rows. SELECTCOUNT(*)FROMSales. Target; -- 1. 46.
If we try to switch again we will get an error: ALTERTABLESales. Source. SWITCHTOSales. Target; -- Msg 4. ALTER TABLE SWITCH statement failed. The target table 'Sales. Target' must be empty.- - But if we try to switch back to the now empty Source table, it works: ALTERTABLESales.
Target. SWITCHTOSales. Source; -- (.. STILL SUPER FAST!)2. Load data by switching in: Switch from Non- Partitioned to Partition. The second way to use the ALTER TABLE SWITCH statement is to switch all the data from a non- partitioned table to an empty specified partition in a partitioned table. ALTER TABLE Source SWITCH TO Target PARTITION 1. ALTERTABLESource. SWITCHTOTarget. PARTITION1.
Before switch: After switch: This is usually referred to as switching in to load data into partitioned tables. The non- partitioned table must specify WITH CHECK constraints to ensure that the data can be switched into the specified partition.
Drop objects if they already exist. IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales. Source'). DROP TABLE Sales. Source. IF EXISTS (SELECT * FROM sys.
WHERE name = N'Sales. Target'). DROP TABLE Sales.
Target. IF EXISTS (SELECT * FROM sys. We get an error. - - Msg 4. ALTER TABLE SWITCH statement failed. Check constraints of source table 'Sales. Source'. - - allow values that are not allowed by range defined by partition 1 on target table 'Sales'.
Is it really that fast..? We get an error: -- Msg 4. ALTER TABLE SWITCH statement failed.
Check constraints of source table 'Sales. Source' - - allow values that are not allowed by range defined by partition 1 on target table 'Sales'.- - Add constraints to the source table to ensure it only contains data with values - - that are allowed in partition 1 on the target table. ALTERTABLESales. Source.
WITHCHECKADDCONSTRAINTck. Min. Sales. Date. CHECK(Sales. Date. ISNOTNULLANDSales. Date> ='2. 01. ALTERTABLESales. Source.
WITHCHECKADDCONSTRAINTck. Max. Sales. Date. CHECK(Sales. Date.
ISNOTNULLANDSales. Date< '2. 01. 3- 0. Try again. Is it really that fast..? ALTERTABLESales. Source.
SWITCHTOSales. Target. PARTITION1; -- YEP! SUPER FAST!- - Turn off statistics. SETSTATISTICSTIMEOFF; -- Verify row count after switch. SELECTCOUNT(*)FROMSales.
Source; -- 0 rows. SELECT pstats. partition.
Archive data by switching out: Switch from Partition to Non- Partitioned. The third way to use the ALTER TABLE SWITCH statement is to switch all the data from a specified partition in a partitioned table to an empty non- partitioned table. ALTER TABLE Source SWITCH PARTITION 1 TO Target. ALTERTABLESource. SWITCHPARTITION1.
TOTarget. Before switch: After switch: This is usually referred to as switching out to archive data from partitioned tables. Drop objects if they already exist.
IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales. Source'). DROP TABLE Sales. Source. IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales.
Target'). DROP TABLE Sales. Target. IF EXISTS (SELECT * FROM sys.
SUPER FAST!- - Turn off statistics. SETSTATISTICSTIMEOFF; -- Verify row count after switch. SELECT pstats. partition.
Switch from Partition to Partition. The fourth way to use the ALTER TABLE SWITCH statement is to switch all the data from a specified partition in a partitioned table to an empty specified partition in another partitioned table. ALTER TABLE Source SWITCH PARTITION 1 TO Target PARTITION 1.
ALTERTABLESource. SWITCHPARTITION1. TOTarget. PARTITION1. Before switch: After switch: This can be used when data needs to be archived in another partitioned table. Drop objects if they already exist. IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales.
Source'). DROP TABLE Sales. Source. IF EXISTS (SELECT * FROM sys. WHERE name = N'Sales. Target'). DROP TABLE Sales. Target. IF EXISTS (SELECT * FROM sys.
SUPER FAST!- - Turn off statistics. SETSTATISTICSTIMEOFF; -- Verify row count after switch. SELECT pstats. partition. You can see all messages related to ALTER TABLE SWITCH by executing the following query, it is also quite a handy requirements checklist.
SELECT message. New data can be loaded to separate tables and then switched in, old data can be switched out to separate tables and then archived or purged. There are many requirements for switching partitions.