$PARTITION (Transact-SQL)
Returns the partition number into which a set of partitioning column values would be mapped for any specified partition function in SQL Server 2012.
A. Getting the partition number for a set of partitioning column values
The following example creates a partition function RangePF1 that will partition a table or index into four partitions. $PARTITION is used to determine that the value 10, representing the partitioning column of RangePF1, would be put in partition 1 of the table.
USE AdventureWorks2012; GO CREATE PARTITION FUNCTION RangePF1 ( int ) AS RANGE FOR VALUES (10, 100, 1000) ; GO SELECT $PARTITION.RangePF1 (10) ; GO
B. Getting the number of rows in each nonempty partition of a partitioned table or index
The following example returns the number of rows in each partition of table TransactionHistory that contains data. The TransactionHistory table uses partition function TransactionRangePF1 and is partitioned on the TransactionDate column.
To execute this example, you must first run the PartitionAW.sql script against the AdventureWorks2012 sample database. For more information, see PartitioningScript.
USE AdventureWorks2012; GO SELECT $PARTITION.TransactionRangePF1(TransactionDate) AS Partition, COUNT(*) AS [COUNT] FROM Production.TransactionHistory GROUP BY $PARTITION.TransactionRangePF1(TransactionDate) ORDER BY Partition ; GO
C. Returning all rows from one partition of a partitioned table or index
The following example returns all rows that are in partition 5 of table TransactionHistory.
Note
|
|---|
|
To execute this example, you must first run the PartitionAW.sql script against the AdventureWorks2012 sample database. For more information, see PartitioningScript. |
SELECT * FROM Production.TransactionHistory WHERE $PARTITION.TransactionRangePF1(TransactionDate) = 5 ;