Skip to content

Latest commit

 

History

History
102 lines (79 loc) · 4.2 KB

File metadata and controls

102 lines (79 loc) · 4.2 KB
title $PARTITION (Transact-SQL) | Microsoft Docs
ms.custom
ms.date 03/06/2017
ms.prod sql-non-specified
ms.reviewer
ms.suite
ms.technology
database-engine
ms.tgt_pltfrm
ms.topic language-reference
f1_keywords
$partition_TSQL
$partition
dev_langs
TSQL
helpviewer_keywords
$PARTITION function
partitions [SQL Server], numbers
ms.assetid abc865d0-57a8-49da-8821-29457c808d2a
caps.latest.revision 39
author BYHAM
ms.author rickbyh
manager jhubbard

$PARTITION (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2008-xxxx-xxxx-xxx_md]

Returns the partition number into which a set of partitioning column values would be mapped for any specified partition function in [!INCLUDEssCurrent].

Topic link icon Transact-SQL Syntax Conventions

Syntax

  
[ database_name. ] $PARTITION.partition_function_name(expression)  

Arguments

database_name
Is the name of the database that contains the partition function.

partition_function_name
Is the name of any existing partition function against which a set of partitioning column values are being applied.

expression
Is an expression whose data type must either match or be implicitly convertible to the data type of its corresponding partitioning column. expression can also be the name of a partitioning column that currently participates in partition_function_name.

Return Types

int

Remarks

$PARTITION returns an int value between 1 and the number of partitions of the partition function.

$PARTITION returns the partition number for any valid value, regardless of whether the value currently exists in a partitioned table or index that uses the partition function.

Examples

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 [!INCLUDEssSampleDBobject] 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 the table TransactionHistory.

Note

To execute this example, you must first run the PartitionAW.sql script against the [!INCLUDEssSampleDBobject] sample database. For more information, see PartitioningScript.

SELECT * FROM Production.TransactionHistory  
WHERE $PARTITION.TransactionRangePF1(TransactionDate) = 5 ;  

See Also

CREATE PARTITION FUNCTION (Transact-SQL)