USE Master;GO--- Step 1 : Create New Test Database with two different filegroups.IF EXISTS (SELECT name FROM sys.databases WHERE name = N'PartitionDB') DROP DATABASE PartitionDB;GOCREATE DATABASE PartitionDB ON PRIMARY (NAME='PartitionDB_Part1',FILENAME= 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\PartitionDB_Part1.mdf',SIZE=3, MAXSIZE=100,FILEGROWTH=1 );GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionDB_Part2GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionDB_Part3GOALTER DATABASE PartitionDB ADD FILEGROUP PartitionDB_Part4GO-------------------------------------------------------------------------------alter database PartitionDB add file (NAME = 'PartitionDB_Part2',FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\PartitionDB_Part2.ndf', SIZE =3,MAXSIZE=100,FILEGROWTH=1 ) to filegroup PartitionDB_Part2; alter database PartitionDB add file (NAME = 'PartitionDB_Part3',FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\PartitionDB_Part3.ndf', SIZE = 3,MAXSIZE=100,FILEGROWTH=1) to filegroup PartitionDB_Part3;GOalter database PartitionDB add file (NAME = 'PartitionDB_Part4',FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\DATA\PartitionDB_Part4.ndf', SIZE = 3,MAXSIZE=100,FILEGROWTH=1) to filegroup PartitionDB_Part4;GOUSE PartitionDB;GO--- Step 2 : Create Partition Range FunctionCREATE PARTITION FUNCTION PartitionDB_PartitionRange (INT) AS RANGE LEFT FOR VALUES (500,1000,2500);GO--- Step 3 : Attach Partition Scheme to FileGroupsCREATE PARTITION SCHEME PartitionDB_PartitionScheme AS PARTITION PartitionDB_PartitionRange TO ([PRIMARY],[PartitionDB_Part2],[PartitionDB_Part3],[PartitionDB_Part4]);GO--- Step 4 : Create Table with Partition Key and Partition SchemeCREATE TABLE TestTable ( ID INT identity ,Date DATETIME ) ON PartitionDB_PartitionScheme (ID);GO--- Step 5 : (Optional/Recommended) Create Index on Partitioned TableCREATE UNIQUE CLUSTERED INDEX IX_TestTable ON TestTable(ID) ON PartitionDB_PartitionScheme (ID);GOdeclare @count int set @count =1 While (@count < = 5000)begin INSERT INTO TestTable (Date) VALUES (GETDATE()); set @count = @count + 1 endGO--- Step 7 : Verify Rows Inserted in PartitionsSELECT *FROM sys.partitionsWHERE OBJECT_NAME(OBJECT_ID)='TestTable';GOCREATE TABLE TestTable_Archive ( ID INT identity ,Date DATETIME ) GOdeclare @count int set @count =1 While (@count < = 40)begin INSERT INTO TestTable_Archive (Date) VALUES (GETDATE()); set @count = @count + 1 endGOCREATE UNIQUE CLUSTERED INDEX IX_TestTable_Archive ON TestTable_Archive(ID) GO
-- make the switch ALTER TABLE TestTable_Archive SWITCH TO TestTable PARTITION 1; GO
We walk in the dark places no others will enterWe stand on the bridge and no one may pass
ALTER TABLE TestTable SWITCH PARTITION 1 TO TestTable_Archive ;
declare @count int set @count =31 While (@count < = 40)begin INSERT INTO TestTable_Archive (id,Date) VALUES (@count,GETDATE()); set @count = @count + 1 endGO
ALTER TABLE TestTable_Archive SWITCH TO TestTable PARTITION 1;