我正在调查的一个数据库在其中定义了一个视图admin.View_PartitionRanges
,该视图主要基于2009PartitionRanges
年这篇 MSDN 博客文章中的视图。那里的视图定义如下:
CREATE VIEW [dbo].[PartitionRanges] AS
select pf.name as [partition_function],
ps.name as [partition_scheme],
1 as [partition_number],
case when prv.value is null then NULL else '<' end as [relation],
prv.value as [boundary_value],
type_name(pp.system_type_id) as [type],
fg.name as [filegroup],
case when ps.name is null then NULL else N'IN USE' end as [status]
from sys.partition_functions pf
join sys.partition_parameters pp on pp.function_id = pf.function_id
left join sys.partition_schemes ps on ps.function_id = pf.function_id
left join sys.destination_data_spaces dds
on dds.partition_scheme_id = ps.data_space_id and dds.destination_id = 1
left join sys.filegroups fg on fg.data_space_id = dds.data_space_id
left join sys.partition_range_values prv
on prv.function_id = pf.function_id and prv.parameter_id = 1 and
prv.boundary_id = 1
where pf.boundary_value_on_right = 1
union all
select pf.name as [partition_function],
ps.name as [partition_scheme],
prv.boundary_id + cast(pf.boundary_value_on_right as int) as [partition_number],
case when pf.boundary_value_on_right = 0 then '<=' else '>=' end as [relation],
prv.value as [boundary_value],
type_name(pp.system_type_id) as [type],
fg.name as [filegroup],
case when ps.name is null then NULL else N'IN USE' end as [status]
from sys.partition_functions pf
join sys.partition_range_values prv on
prv.function_id = pf.function_id and prv.parameter_id = 1
join sys.partition_parameters pp on pp.function_id = pf.function_id
left join sys.partition_schemes ps on ps.function_id = pf.function_id
left join sys.destination_data_spaces dds
on dds.partition_scheme_id = ps.data_space_id and
dds.destination_id = prv.boundary_id + cast(pf.boundary_value_on_right as int)
left join sys.filegroups fg on fg.data_space_id = dds.data_space_id
union all
select pf.name as [partition_function],
ps.name as [partition_scheme],
pf.fanout as [partition_number],
case when prv.value is null then NULL else '>' end as [relation],
prv.value as [boundary_value],
type_name(pp.system_type_id) as [type],
fg.name as [filegroup],
case when ps.name is null then NULL else N'IN USE' end as [status]
from sys.partition_functions pf
join sys.partition_parameters pp on pp.function_id = pf.function_id
left join sys.partition_schemes ps on ps.function_id = pf.function_id
left join sys.destination_data_spaces dds
on dds.partition_scheme_id = ps.data_space_id and dds.destination_id = pf.fanout
left join sys.filegroups fg on fg.data_space_id = dds.data_space_id
left join sys.partition_range_values prv
on prv.function_id = pf.function_id and prv.parameter_id = 1 and
prv.boundary_id = pf.fanout - 1
where pf.boundary_value_on_right = 0
union all
select pf.name as [partition_function],
ps.name as [partition_scheme],
NULL, NULL, NULL, NULL,
fg.name as [filegroup],
case when dds.destination_id = pf.fanout + 1
then N'NEXT USED' else N'NOT USED'
end as [status]
from sys.partition_functions pf
join sys.partition_schemes ps on ps.function_id = pf.function_id
join sys.destination_data_spaces dds
on dds.partition_scheme_id = ps.data_space_id and
dds.destination_id > pf.fanout
join sys.filegroups fg on fg.data_space_id = dds.data_space_id
但是,当我SELECT *
在 SSMS 中对此视图执行操作时,出现错误
An error occurred while executing batch. Error message is:
Input string was not in a correct format.
怎么了?
问题在于该视图使用的来源之一,系统视图
sys.partition_range_values
。这个视图的Value
列是 typeSQL_VARIANT
,这可能会导致不加选择SELECT *
的问题,因为 SQL 必须为结果集选择一个特定的类型,然后所有的值都需要被强制转换为该类型(感谢这篇博文提供了这种见解) .你可以自己看看这是否是导致你的问题的原因,方法是
如果您有多个不同
DataType
的 s,并且您可以想象在它们之间转换值可能会出现问题,那就是问题所在。要修复,您可以更新您的助手视图,而不是
相反你有
这将为您提供分区范围值的适当文本表示。