【问题标题】:Report Builder 3.0 - How can I write SQL that reads all parm names and values?Report Builder 3.0 - 如何编写读取所有参数名称和值的 SQL?
【发布时间】:2018-04-18 18:44:03
【问题描述】:

示例:

参数:parm

标签:ParmLabel

值:value

用户将选择他们想要的参数值,然后运行报告。当报告加载时,我想在报告上显示一个 URL 字符串,其中包含所有参数和他们选择的值。我该怎么做?

棘手的部分:

  1. 有很多参数,因此为每个参数手动编码会花费太长时间。需要一种循环方式。

  2. 有些参数可能留空,有些可能为 NULL

  3. 所有参数都可以有不同的数据类型,因此解决方案需要独立于数据类型。

我尝试过的:

这行得通,但对每个报告和该报告中的每个参数都这样做是不现实的。这种方法要求我进入 DataSet 属性并将多值参数的值作为逗号分隔的值字符串传递,这很烦人。如果可能的话,我也想避免这种情况。

select

    (@URL 

        + 

            case 
                when len(@parm) <> 0 --This works. The query skips the parm if no values (or only blank values) are passed.
                then '&parm=' + replace(@parm, ',', '&parm=')
                else '' --Parm has no values
                end 

    ) as URLString

【问题讨论】:

    标签: sql reporting-services sql-server-2008-r2 report reportbuilder3.0


    【解决方案1】:

    我可以在这里为您想到一个技巧。这是未经测试的,可能需要完善。

    1) 首先构建一个数据集以返回一个动态表查询,并在数据集中添加一个类似于下面的查询。这应该生成一个包含 X 条记录的表。这个想法是将 X 设置为用户在您的报告中使用的参数数量。

    DECLARE @RowCount INT=4
    ;WITH T AS( 
        SELECT N=1 
        UNION ALL 
        SELECT N + 1 FROM T WHERE N + 1 <= @RowCount 
    ) 
    SELECT RowNumber=N FROM T 
    OPTION (MAXRECURSION 255);
    

    2) 将 tablix 或表格添加到您的报告中,并将数据源设置为上面创建的数据源。

    3) 在步骤 2 中创建的表格中添加参数详细信息的标签。

    4) 添加自定义代码过程以返回每个参数的详细信息。

    Public Function GetParameter(ByVal parameters as Parameters, ByVal index as Integer) as Parameter
        Return parameters(index)
    End Function
    

    5) 添加自定义代码过程以返回参数计数。

    Public Function GetParameterCount(ByVal parameters as Parameters) as Integer
        Return parameters.Count()
    End Function
    

    6) 用上面的计数填充行计数参数

    7) 使用表达式填充标签

    =Code.GetParameter(Parameters,Fields!RowNumber).Value
    =Code.GetParameter(Parameters,Fields!RowNumber).Name
    =Code.GetParameter(Parameters,Fields!RowNumber).DisplayLabel
    ...
    

    MSDN Documentation on accessing Report Parameters in Custom Code

    【讨论】:

    • 我很难用这个解决方案清除错误。你能帮我度过难关吗?
    • 到目前为止你得到了什么?
    • 我已经尝试了许多不同的代码变体,但我没有保存我为每次尝试所做的事情。我将再试一次,并列出我在这里遇到的错误。
    • 第一个错误:The Value expression for the text box ‘Textbox14’ refers to the field ‘RowCount’. Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope. Letters in the names of fields must use the correct case. 为了解决这个问题,我将它从Fields!RowCount 更改为Fields!RowNumber
    • 我得到的下一个错误是:There is an error on line 5 of custom code: [BC30456] 'Count' is not a member of 'Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Parameters'.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多