I am using switch table sometime on SQL Server 2008 to transfer quickly data between 2 tables, but I do not know how it works, and what's the difference between this an just a insert into T2 select * from T1
.
Someone can explain in detail how it works and the difference?
Furthermore I notice that if both tables are not clones this statement does not work. For example if I have one index in one table and not in the other one it fails.
Inserting data using the statement
insert into T2 select * from T1
Loads data by inserting data from T1 into T2. When a partition is transferred (switched), the data is not physically moved; only the metadata about the location of the data changes.
Before you can switch partitions, several general requirements must be met:
General Requirements for Switching Partitions
When a partition is transferred, the data is not physically moved; only the metadata about the location of the data changes. Before you can switch partitions, several general requirements must be met:
Both tables must exist before the SWITCH operation. The table from which the partition is being moved (the source table) and the table that is receiving the partition (the target table) must exist in the database before you perform the switch operation.
The receiving partition must exist and it must be empty. Whether you are adding a table as a partition to an already existing partitioned table, or moving a partition from one partitioned table to another, the partition that receives the new partition must exist and it must be an empty partition.
The receiving nonpartitioned table must exist and it must be empty. If you are reassigning a partition to form one nonpartitioned table, the table that receives the new partition must exist and it must be an empty nonpartitioned table.
Partitions must be on the same column. If you are switching a partition from one partitioned table to another, both tables must be partitioned on the same column.
Source and target tables must share the same filegroup. The source and the target table of the ALTER TABLE...SWITCH statement must reside in the same filegroup, and their large-value columns must be stored in the same filegroup. Any corresponding indexes, index partitions, or indexed view partitions must also reside in the same filegroup. However, the filegroup can be different from that of the corresponding tables or other corresponding indexes.
This info and more is located here:
https://technet.microsoft.com/en-us/library/ms191160(v=sql.105).aspx