使用字符串函数

你几乎可以在多维表达式(MDX)中对每个对象使用字符串函数。 在存储过程中,你主要使用字符串函数来将对象转换为字符串表示。 你还会用字符串函数来对对象计算字符串表达式,从而返回一个值。

最广泛使用的字符串函数是 NameUniquename。 这些函数分别返回对象的名称和唯一名称。 它们主要用于调试计算,以发现函数返回的成员。

示例

以下示例查询展示了如何使用这些函数:

WITH  
//Returns the name of the current Product on rows  
MEMBER [Measures].[ProductName] AS [Product].[Product].CurrentMember.Name  
//Returns the uniquename of the current Product on rows  
MEMBER [Measures].[ProductUniqueName] AS [Product].[Product].CurrentMember.Uniquename  
//Returns the name of the Product dimension  
MEMBER [Measures].[ProductDimensionName] AS [Product].Name  
SELECT  {[Measures].[ProductName],[Measures].[ProductUniqueName],[Measures].[ProductDimensionName]}  
ON COLUMNS,  
[Product].[Product].MEMBERS  ON ROWS  
FROM [Adventure Works]  

生成 函数可用于 对集合中的每个成员执行字符串函数并串接结果。 这在调试计算时也很有用,因为它让你能够可视化集合的内容。 以下示例展示了如何以此方式使用:

WITH  
//Returns the names of the current Product and its ancestors up to the All Member  
MEMBER [Measures].[AncestorNames] AS  
GENERATE(  
ASCENDANTS([Product].[Product Categories].CurrentMember)  
, [Product].[Product Categories].CurrentMember.Name, ", ")  
SELECT  
{[Measures].[AncestorNames]}  
ON COLUMNS,  
[Product].[Product Categories].MEMBERS  ON ROWS  
FROM [Adventure Works]  

另一类广泛使用的字符串函数是那些允许你将包含对象唯一名称的字符串或一个表达式(向对象解析)铸造到对象本身的函数。 以下示例查询展示了 StrToMemberStrToSet 函数如何实现这一点:

SELECT  
{StrToMember("[Measures].[Inter" + "net Sales Amount]")}  
ON COLUMNS,  
StrToSet("{  
[Product].[Product Categories].[Category].&[3],  
[Product].[Product Categories].[Product].&[477],  
[Product].[Product Categories].[Product].&[788],  
[Product].[Product Categories].[Product].&[708],  
[Product].[Product Categories].[Product].&[711]  
}")  
ON ROWS  
FROM [Adventure Works]  

Note

StrToMemberStrToSet函数应谨慎使用。 如果在计算定义中使用它们,可能会导致查询性能下降。