mvc中嵌入ssrs报表
Ever since the early days of my career, SQL Server Reporting Services (SSRS) has been one of my preferred data visualization tools simply because end users and developers alike use it for free. Although a majority of my SSRS solutions have been based off a relational dataset that uses Transact SQL (T-SQL), I have also produced several reports that used Multidimensional Expressions (MDX) to connect and retrieve data from SQL Server Analysis Services (SSAS) multidimensional OLAP cube. Recently, I found myself having to refactor some of these SSAS based SSRS reports, particularly converting a single value SSAS-populated parameter into a multi-value parameter. In this article, I explore how you can go about making these changes using SSRS query designer’s design and query modes.
从我职业生涯的早期开始,SQL Server Reporting Services(SSRS)就一直是我首选的数据可视化工具之一,因为最终用户和开发人员都可以免费使用它。 尽管我的大多数SSRS解决方案都基于使用Transact SQL(T-SQL)的关系数据集,但我也产生了一些使用多维表达式(MDX)连接并从SQL Server Analysis Services(SSAS)检索数据的报告。多维OLAP多维数据集。 最近,我发现自己不得不重构一些基于SSAS的SSRS报告,尤其是将单值SSAS填充的参数转换为多值参数。 在本文中,我将探讨如何使用SSRS查询设计器的设计和查询模式进行这些更改。
在设计模式下使用查询参数 (Working with query parameters in design mode)
Just like using a relational database to define your report dataset requires an understanding of T-SQL, referencing multidimensional OLAP cube data requires a basic understanding of MDX. However, you can circumvent the MDX learning process by using a GUI – Graphical User Interface – to build and prepare your report dataset. In the context of SSRS, the GUI includes a dataset query designer’s design mode. It involves navigating to a new dataset, choosing your SSAS data source and then clicking and dragging the measures and dimensions you want to report on. In my FruitSales demo cube shown in Figure 1, I have chosen Count as my measure and Fruit and MOP (method of payment), as my dimensions.
就像使用关系数据库来定义报告数据集一样,您需要了解T-SQL,引用多维OLAP多维数据集数据也需要对MDX有基本的了解。 但是,您可以使用GUI(图形用户界面)来构建和准备报告数据集,从而规避MDX学习过程。 在SSRS的上下文中,GUI包括数据集查询设计者的设计模式。 它涉及导航到新的数据集,选择SSAS数据源,然后单击并拖动要报告的度量和维。 在图1所示的FruitSales演示多维数据集中,我选择了Count作为度量值,并选择了Fruit和MOP (付款方式)作为维度。
Adding parameters to our dataset using the designer is also effortless. All you need to do is navigate to the top right filter section of the designer, specify the dimension you want to filter on and check the box underneath the Parameters field. In this example, I have chosen to use MOP as a filter with a default value of CASH. The rest of the dataset looks as shown in Figure 2.
使用设计器向我们的数据集添加参数也是轻松的。 您需要做的只是导航到设计器的右上方过滤器部分,指定要过滤的尺寸,然后选中“参数”字段下面的框。 在此示例中,我选择将MOP用作默认值为CASH的过滤器。 数据集的其余部分如图2所示。
Once you close the query designer, you get to see an MDX script that was generated based on the selections you made. The script generated based on Figure 2 is shown in Script 1.
关闭查询设计器后,您将看到基于所做选择生成的MDX脚本。 基于图2生成的脚本显示在脚本1中 。
SELECT non empty { [Measures].[Count] }
ON columns,
non empty { ([DimFruits].[Fruit].[Fruit].ALLMEMBERS * [DimFruits].[MOP].[MOP].ALLMEMBERS ) } dimension properties member_caption,
member_unique_name
ON rows
FROM (
SELECT ( strtoset(@DimFruitsMOP, constrained) )
ON columns
FROM [FruitsSales]) cell properties value,
back_color,
fore_color,
formatted_value,
format_string,
font_name,
font_size,
font_flags
Having successfully set up a SSAS dataset using GUI, we can proceed to develop a report and Figure 3 shows us a preview of the newly developed report.
使用GUI成功设置了SSAS数据集之后,我们可以继续开发报告, 图3为我们展示了新开发的报告的预览。
![](https://www.sqlshack.com/wp-content/uploads/2016/09/word-image-374.png)
Although the GUI makes it easy for novice developers to set up a parameterized SSRS report that is based off a multidimensional cube, there are several drawbacks to this approach:
尽管GUI使新手开发人员可以轻松地基于多维多维数据集来设置参数化的SSRS报告,但是此方法有一些缺点:
The auto-generated script (in Script 1) includes unnecessary formatting information (i.e. BACK_COLOR, FORE_COLOR) that is embedded in the query – which makes the script untidy and not easy to read.
自动生成的脚本(在脚本1中)包括嵌入在查询中的不必要的格式信息(即BACK_COLOR,FORE_COLOR ),这使脚本不整洁且不易于阅读。
The dataset parameter name (and subsequently, report parameter name) is not business friendly as it is derived from a concatenation of dimension name (DimFruits) and dimension member (MOP).
数据集参数名称(以及随后的报告参数名称)不是业务友好的,因为它是由维名称( DimFruits )和维成员( MOP )的串联而派生的。
Another issue relating to parameter is that parameter values contains an MDX notation (i.e. [Dimension].[Member].&[Value]) that is not business friendly.
与参数有关的另一个问题是,参数值包含不友好的MDX表示法(即[Dimension]。[Member]。&[Value] )。
在查询模式下使用查询参数 (Working with query parameters in query mode)
The drawbacks mentioned in the preceding section can be resolved by writing your own MDX script which gives you control of the dataset and ultimately the report. To edit MDX script we will switch from design mode into query mode.
上一节中提到的缺点可以通过编写自己的MDX脚本来解决,该脚本使您可以控制数据集并最终控制报表。 要编辑MDX脚本,我们将从设计模式切换到查询模式。
Single-value parameters using STRTOMEMBER
使用STRTOMEMBER的单值参数
MDX has several built-in functions that can be useful when it comes to parameterising query datasets. One of those functions is the STRTOMEMBER which returns a member specification. Script 2 shows a refactored version of Script 1 that makes use of the STRTOMEMBER function.
MDX具有多个内置函数,在参数化查询数据集时非常有用。 这些函数之一是STRTOMEMBER ,它返回成员规范。 脚本2显示了使用STRTOMEMBER函数的脚本1的重构版本。
SELECT NON EMPTY { [Measures].[Count] } ON COLUMNS, NON EMPTY { ( [DimFruits].[Fruit].[Fruit].AllMembers ) * { StrToMember ( @MOP ) } } ON ROWS FROM [FruitsSales]
Script 2: MDX Script Using STRTOMEMBER
脚本2:使用STRTOMEMBER的 MDX脚本
Script 2 makes references to a parameter called @MOP. Unlike in design view, this parameter will not be automatically added into the dataset instead we have to manually add it. Figure 4 shows the definition of the @MOP parameter with a default value of CASH.
脚本2引用了一个名为@MOP的参数。 与设计视图不同,此参数不会自动添加到数据集中,而是我们必须手动添加它。 图4显示了@MOP参数的定义,其默认值为CASH 。
Figure 4 图4 Following the changes made to our shared dataset, a preview of our demo report is shown in Figure 5. In terms of the layout, not much has changed since we updated the script to use STRTOMEMBER except for the parameter name which is no longer prefixed by a dimension name.
更改共享数据集后,演示报告的预览如图5所示。 在布局方面,自从我们更新脚本以使用STRTOMEMBER以来,除了参数名称不再以尺寸名称作为前缀之外,没有太大变化。
Figure 5 图5 Evidently, we still have an issue of a predefined parameter value that contains MDX expression. We need to refactor this in such a way that it displays member value only (i.e. CASH, ELECTRONIC etc.). We can do this by having the MOP parameter populated by a separate lookup dataset. Script 3 shows an MDX script for a dataset that will be used to populate the MOP parameter.
显然,仍然存在包含MDX表达式的预定义参数值的问题。 我们需要重构它,使其仅显示成员值(即现金,电子等)。 我们可以通过由单独的查找数据集填充MOP参数来实现。 脚本3显示了将用于填充MOP参数的数据集的MDX脚本。
WITH MEMBER [Measures].[Label] AS [DimFruits].[MOP].currentmember.member_caption MEMBER [Measures].[Value] AS [DimFruits].[MOP].currentmember.uniquename SELECT { [Measures].[Label], [Measures].[Value] } ON COLUMNS, [DimFruits].[MOP].allmembers ON ROWS FROM [FruitsSales]
Script 3: MOP Lookup MDX Query 脚本3:MOP查找MDX查询 The updated report which references the newly added dataset is shown in Figure 6. As it can be seen, this view of the report allows end users to choose the method of payment without having to specify an MDX expression.
引用了新添加的数据集的更新报告如图6所示。 可以看出,该报告视图允许最终用户选择付款方式,而无需指定MDX表达式。
Figure 6 图6 Multi-value parameters using STRTOSET
使用STRTOSET的多值参数
STRTOMEMBER function resolved several limitations of an MDX script generated via design mode. Unfortunately, STRTOMEMBER function has its own limitation which is – it doesn’t allow us to choose multiple parameter values. For instance, if we attempt to choose multiple MOPs, we receive an error as shown in Figure 7.
STRTOMEMBER函数解决了通过设计模式生成的MDX脚本的一些限制。 不幸的是, STRTOMEMBER函数有其自身的局限性,即它不允许我们选择多个参数值。 例如,如果我们尝试选择多个MOP,则会收到如图7所示的错误。
Figure 7: STRTOMEMBER function expects a member expression error 图7:STRTOMEMBER函数期望成员表达式错误 In order to address this limitation of STRTOMEMBER function, we make use of another MDX built-in function –STRTOSET. The revised script that uses STRTOSET function is shown in Script 4.
为了解决STRTOMEMBER函数的这一限制,我们使用了另一个MDX内置函数STRTOSET 。 脚本4中显示了使用STRTOSET函数的修订脚本。
SELECT non empty { [Measures].[Count] } ON columns, non empty { ([DimFruits].[Fruit].[Fruit].ALLMEMBERS) * {strtoset(@MOP)} } ON rows FROM [FruitsSales]
STRTOMEMBER STRTOMEMBER的 MDX脚本 The MOP parameter was also modified to include multi-values as shown in Figure 8.
如图8所示,还修改了MOP参数以包含多值。
Figure 8 图8 A preview of the updated report that uses the script that references STRTOSET is shown in Figure 9. As it can be seen, it is now possible to choose multiple methods of payments without breaking report execution.
图9显示了使用引用STRTOSET的脚本的更新报告的预览。 可以看出,现在可以选择多种付款方式而不会影响报告的执行。
Figure 9 图9
结论 (Conclusion)
In this article, we have demonstrated that unlike using T-SQL, populating an SSRS dataset using a multidimensional OLAP cube requires, at the very least, a basic understanding of MDX. We also demonstrated that out of the two modes, design and query mode, the query mode gives you more control of the dataset properties and handling of query parameters using the STRTOMEMBER and STRTOSET built-in MDX functions.
在本文中,我们证明了与使用T-SQL不同,使用多维OLAP多维数据集填充SSRS数据集至少需要对MDX有基本的了解。 我们还演示了在设计和查询两种模式中,查询模式使用STRTOMEMBER和STRTOSET内置MDX函数使您可以更好地控制数据集属性和查询参数的处理。
参考资料 (References)
- StrToMember (MDX)StrToMember(MDX)
- StrToSet (MDX)StrToSet(MDX)
- Multidimensional Expressions (MDX)多维表达式(MDX)
翻译自: https://www.sqlshack.com/filter-multidimensional-olap-cubes-ssrs-reports/
mvc中嵌入ssrs报表