--
--Chapter 12 SQL code for the partitoned example.
--
USE AdventureWorks2008;
-- Create the partition function
CREATE PARTITION FUNCTION [OrderDateRangePFN](datetime)
AS RANGE RIGHT
FOR VALUES (N'2001-01-01 00:00:00', N'2002-01-01 00:00:00',
N'2003-01-01 00:00:00', N'2004-01-01 00:00:00');
-- Create the partition scheme
CREATE PARTITION SCHEME [OrderDatePScheme]
AS PARTITION [OrderDateRangePFN]
TO ([Primary], [Primary], [Primary], [Primary], [Primary]);
-- Create two tables: SalesOrderHeader is the partitioned table and SalesOrderHeaderOLD is the non-partitioned table
CREATE TABLE [dbo].[SalesOrderHeader](
[SalesOrderID] [int] NULL,
[RevisionNumber] [tinyint] NOT NULL,
[OrderDate] [datetime] NOT NULL,
[DueDate] [datetime] NOT NULL,
[ShipDate] [datetime] NULL,
[Status] [tinyint] NOT NULL
) ON [OrderDatePScheme]([OrderDate]);
CREATE TABLE [dbo].[SalesOrderHeaderOLD](
[SalesOrderID] [int] NULL,
[RevisionNumber] [tinyint] NOT NULL,
[OrderDate] [datetime] NOT NULL ,
[DueDate] [datetime] NOT NULL,
[ShipDate] [datetime] NULL,
[Status] [tinyint] NOT NULL);
-- Load daata into the partitioned table
INSERT INTO SalesOrderHeader
SELECT [SalesOrderID],[RevisionNumber], [OrderDate],[DueDate],[ShipDate],[Status] FROM SALES.[SalesOrderHeader];
CREATE CLUSTERED INDEX SalesOrderHeaderCLInd
ON SalesOrderHeader(OrderDate) ON OrderDatePScheme(OrderDate);
CREATE CLUSTERED INDEX SalesOrderHeaderOLDCLInd ON SalesOrderHeaderOLD(OrderDate);
ALTER TABLE [DBO].[SalesOrderHeaderOLD] WITH CHECK ADD CONSTRAINT [CK_SalesOrderHeaderOLD_ORDERDATE] CHECK ([ORDERDATE]>=('2003-01-01 00:00:00') AND [ORDERDATE]<('2004-01-01 00:00:00'));
-- Verify data in the partitioned table
SELECT $partition.OrderDateRangePFN(OrderDate) AS 'Partition Number' ,min(OrderDate) AS 'Min Order Date' , max(OrderDate) AS 'Max Order Date',count(*) AS 'Rows In Partition'
FROM SalesOrderHeader
GROUP BY $partition.OrderDateRangePFN(OrderDate);
-- Switch the data from partition 4 into the SalesOrderHeaderOLD table
ALTER TABLE SalesOrderHeader
SWITCH PARTITION 4 TO SalesOrderHeaderOLD;
-- Switch the data from SalesOrderHeaderOLD back to partition 4
ALTER TABLE SalesOrderHeaderOLD
SWITCH TO SalesOrderHeader PARTITION 4;
--To merge a partition range
ALTER PARTITION FUNCTION OrderDateRangePFN()
MERGE RANGE ('2003-01-01 00:00:00');
-- To split a partition range
ALTER PARTITION SCHEME OrderDatePScheme NEXT USED [Primary];
ALTER PARTITION FUNCTION OrderDateRangePFN()
SPLIT RANGE ('2003-01-01 00:00:00');
--Chapter 12 SQL code for the partitoned example.
--
USE AdventureWorks2008;
-- Create the partition function
CREATE PARTITION FUNCTION [OrderDateRangePFN](datetime)
AS RANGE RIGHT
FOR VALUES (N'2001-01-01 00:00:00', N'2002-01-01 00:00:00',
N'2003-01-01 00:00:00', N'2004-01-01 00:00:00');
-- Create the partition scheme
CREATE PARTITION SCHEME [OrderDatePScheme]
AS PARTITION [OrderDateRangePFN]
TO ([Primary], [Primary], [Primary], [Primary], [Primary]);
-- Create two tables: SalesOrderHeader is the partitioned table and SalesOrderHeaderOLD is the non-partitioned table
CREATE TABLE [dbo].[SalesOrderHeader](
[SalesOrderID] [int] NULL,
[RevisionNumber] [tinyint] NOT NULL,
[OrderDate] [datetime] NOT NULL,
[DueDate] [datetime] NOT NULL,
[ShipDate] [datetime] NULL,
[Status] [tinyint] NOT NULL
) ON [OrderDatePScheme]([OrderDate]);
CREATE TABLE [dbo].[SalesOrderHeaderOLD](
[SalesOrderID] [int] NULL,
[RevisionNumber] [tinyint] NOT NULL,
[OrderDate] [datetime] NOT NULL ,
[DueDate] [datetime] NOT NULL,
[ShipDate] [datetime] NULL,
[Status] [tinyint] NOT NULL);
-- Load daata into the partitioned table
INSERT INTO SalesOrderHeader
SELECT [SalesOrderID],[RevisionNumber], [OrderDate],[DueDate],[ShipDate],[Status] FROM SALES.[SalesOrderHeader];
CREATE CLUSTERED INDEX SalesOrderHeaderCLInd
ON SalesOrderHeader(OrderDate) ON OrderDatePScheme(OrderDate);
CREATE CLUSTERED INDEX SalesOrderHeaderOLDCLInd ON SalesOrderHeaderOLD(OrderDate);
ALTER TABLE [DBO].[SalesOrderHeaderOLD] WITH CHECK ADD CONSTRAINT [CK_SalesOrderHeaderOLD_ORDERDATE] CHECK ([ORDERDATE]>=('2003-01-01 00:00:00') AND [ORDERDATE]<('2004-01-01 00:00:00'));
-- Verify data in the partitioned table
SELECT $partition.OrderDateRangePFN(OrderDate) AS 'Partition Number' ,min(OrderDate) AS 'Min Order Date' , max(OrderDate) AS 'Max Order Date',count(*) AS 'Rows In Partition'
FROM SalesOrderHeader
GROUP BY $partition.OrderDateRangePFN(OrderDate);
-- Switch the data from partition 4 into the SalesOrderHeaderOLD table
ALTER TABLE SalesOrderHeader
SWITCH PARTITION 4 TO SalesOrderHeaderOLD;
-- Switch the data from SalesOrderHeaderOLD back to partition 4
ALTER TABLE SalesOrderHeaderOLD
SWITCH TO SalesOrderHeader PARTITION 4;
--To merge a partition range
ALTER PARTITION FUNCTION OrderDateRangePFN()
MERGE RANGE ('2003-01-01 00:00:00');
-- To split a partition range
ALTER PARTITION SCHEME OrderDatePScheme NEXT USED [Primary];
ALTER PARTITION FUNCTION OrderDateRangePFN()
SPLIT RANGE ('2003-01-01 00:00:00');