Sql-server – Obtaining the next partition number

partitioningsql serversql-server-2012

For a partitioned table, how do I determine via T-SQL what is the next partition number that will be used? For example, the switch-in today went into partition number 50. I want to build the new switch-in table to contain the boundary values for partition number 51. I'm currently doing this manually and want to automate it via stored procedures. I'm using SQL Server 2012.

Best Answer

There are a few good DMVs (system views) where you can find this info: sys.partition_functions and sys.partition_schemes can be joined to sys.partition_range_values to get all existing ranges. The number of ranges should be the number of partitions.

The data_space_id of the partition scheme can be joined to that of the index or heap in sys.indexes on the table (sys.tables) that you're interested in.