简体   繁体   English

将多个值传递给子报告参数 SSRS

[英]Passing multiple values into sub report parameter SSRS

I have a report which shows product details.我有一份显示产品详细信息的报告。 Some of these products will be Children of Parent products.其中一些产品将是父母产品的孩子。 The children have 3 columns Parent1 Parent2 and Parent3 .孩子有 3 列Parent1 Parent2Parent3

The sub report runs off the same query except has a variable @ParentCodes .除了有一个变量@ParentCodes之外,子报告运行相同的查询。 This is set as varchar(max) .这被设置为varchar(max) I then use a function in the where clause to split the codes which may be passed ParentCode in (SELECT [Value] FROM dbo.SplitMultiValueParameterString(@ParentCodes, ','))然后我在 where 子句中使用一个函数来拆分可以ParentCode in (SELECT [Value] FROM dbo.SplitMultiValueParameterString(@ParentCodes, ','))传递ParentCode in (SELECT [Value] FROM dbo.SplitMultiValueParameterString(@ParentCodes, ','))的代码

If I pass in 2 or 3 codes in a string like ('123', '456') then the query returns the 2 rows required.如果我在像('123', '456')这样的字符串中传入 2 或 3 个代码,那么查询将返回所需的 2 行。

I am struggling to get this to work in SSRS.我正在努力让它在 SSRS 中工作。 I have set up both reports and added the @ParentCodes parameter to the sub report.我已经设置了两个报告并将@ParentCodes参数添加到子报告中。

In the main report, I am using an action on a text box to go to the sub report and passing the parameter @ParentCodes with the following value expression =Fields!Parent1.Value & Fields!Parent2.Value & Fields!Parent3.Value在主报告中,我使用文本框上的操作转到子报告并使用以下值表达式传递参数@ParentCodes =Fields!Parent1.Value & Fields!Parent2.Value & Fields!Parent3.Value

In the sub report, in the parameter tab in the dataset I have added the @ParentCodes parameter and using the expression =JOIN(Parameters!ParentCodes, ",")在子报告中,在数据集中的参数选项卡中,我添加了@ParentCodes参数并使用表达式=JOIN(Parameters!ParentCodes, ",")

This is not working as desired, can anyone understand what I am trying to do and propose a solution?这不能按预期工作,任何人都可以理解我正在尝试做什么并提出解决方案吗?

In SSRS, if you are using direct statements (in that you just dump the raw SQL into the Report Data) then you need to use an IN , and SSRS (as much as I hate it) "safely" replaces the variable in the IN with an injection of the parameters.在 SSRS 中,如果您使用直接语句(因为您只是将原始 SQL 转储到报告数据中),那么您需要使用IN ,并且 SSRS(尽管我讨厌它)“安全地”替换了IN的变量注入参数。 In simple terms, take a query like the below:简单来说,进行如下查询:

SELECT *
FROM dbo.YourTable
WHERE ID IN (@ID);

Then you select the parameters, 1 , 2 , 3 , and 4 in the report.然后在报告中选择参数1234 It would inject those and the query would become the below:它将注入这些,查询将变为以下内容:

SELECT *
FROM dbo.YourTable
WHERE ID IN (1,2,3,4);

If you're using a Stored Procedure, however, you need to use a string splitter (such as DelimitedSplit8k_LEAD ) to do the work, and make your parameter a (n)varchar :但是,如果您使用的是存储过程,则需要使用字符串拆分器(例如DelimitedSplit8k_LEAD )来完成这项工作,并使您的参数成为(n)varchar

CREATE PROC dbo.YourProc @IDs varchar(8000) AS
BEGIN

SELECT *
FROM dbo.YourTable YT
     JOIN dbo.DelimitedSplit8k_LEAD(@IDs, ',') DS ON YT.ID = DS.item;

It looks like you're expecting the query to do dynamic SQL in the sub report which won't work, but SSRS should do what you need.看起来您希望查询在子报告中执行动态 SQL,但这不起作用,但 SSRS 应该执行您需要的操作。

Try This with your subreport with some multiple parent codes.试试与一些多个父代码子报表。

In your where clause in the subreport you should be able to do this:在子报告中的 where 子句中,您应该能够执行以下操作:

WHERE parentcode IN (@parentcodes) WHERE parentcode IN (@parentcodes)

You may need to tweak your JOIN expression as per this : article but it might just work.您可能需要根据这篇文章调整您的 JOIN 表达式,但它可能会起作用。

If I've understood you correctly, the subreport accepts a single comma separated string as a parameter and that gets populated from the main report?如果我理解正确的话,子报告接受一个逗号分隔的字符串作为参数,并从主报告中填充?

If that is the case then your expression如果是这样,那么你的表达

=Fields!Parent1.Value & Fields!Parent2.Value & Fields!Parent3.Value

is wrong as it does not have any separators so 123 and 456 would be sent as 123456.是错误的,因为它没有任何分隔符,因此 123 和 456 将作为 123456 发送。

You probably need something like你可能需要类似的东西

=Fields!Parent1.Value & "," & Fields!Parent2.Value & "," & Fields!Parent3.Value

The other option would be to just pass in the child product's ID and then get the parent ID's in the subreport's query.另一种选择是只传入子产品的 ID,然后在子报表的查询中获取父 ID。 You would not need to do any kind of processing to build or split out parameter values this way.您不需要进行任何类型的处理来以这种方式构建或拆分参数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM