https://www.cnblogs.com/chuncn/archive/2009/02/20/1395165.html
SQL Server 2005 的分區表(partition table)是復雜的,特別是對於初學者來說。不管怎樣,我們還是掌握了分區函數(partition function),分區方案(partition scheme),最后終於創建了一個分區表出來。但之后呢,或許你想查看分區表的各個分區分區列的取值范圍(這個分區的理論最小值和最大值),雖然可以通過分區函數中定義的邊界值(boundary value)來推算出來分區表每個分區(partition)的取值范圍,但是事情並不是很簡單,你需要一系列繁瑣的步驟來實現:首先要找出這個分區表的分區函數;然后確定該分區函數定義的邊界值屬於 left 或者 right; 最后在腦子里使勁思索幾下終於確定了分區列取值范圍(partition range)。幸運的是,有了下面的這個存儲過程,你的大腦就可以獲得解放了。
if exists (select 1 from sys.procedures where name = 'sp_show_partition_range') drop procedure dbo.sp_show_partition_range go -------------------------------------------------------------------------------- -- author : p.c.w.l -- source : www.sqlstudy.com -- create : 2008-01-01 -- descr : view partition range by 'partition table' or 'partition function' -------------------------------------------------------------------------------- create procedure dbo.sp_show_partition_range ( @partition_table nvarchar(255) = null ,@partition_function nvarchar(255) = null ) as begin set nocount on declare @function_id int set @function_id = null -- get @function_id base on @partition_table if len(@partition_table) > 0 begin select @function_id = s.function_id from sys.indexes i inner join sys.partition_schemes s on i.data_space_id = s.data_space_id where
-- i.index_id < 2 and
i.object_id = object_id(@partition_table) if @function_id is null return 1 end -- get @function_id base on @partition_function if len(@partition_function) > 0 begin select @function_id = function_id from sys.partition_functions where name = @partition_function if @function_id is null return 1 end -- get partition range select partition_function = f.name ,t.partition ,t.minval ,value = case when f.boundary_value_on_right=1 then '<= val <' else '< val <=' end ,t.maxval from ( select h.function_id ,partition = h.boundary_id ,minval = l.value ,maxval = h.value from sys.partition_range_values h left join sys.partition_range_values l on h.function_id = l.function_id and h.boundary_id = l.boundary_id + 1 union all select function_id ,partition = max(boundary_id) + 1 ,minval = max(value) ,maxval = null from sys.partition_range_values group by function_id ) t inner join sys.partition_functions f on t.function_id = f.function_id where f.function_id = @function_id or @function_id is null order by 1, 2 end go