SSRS loop for report and subreport

2019-09-12 23:14发布

问题:

So I have a report with a matching subreport, based on two joined tables. The value: NameID from the one table is the parameter I wish to filter on.

The main table draws data from a query like this:

SELECT * 
FROM NameTable
Where NameID = '111'

The subreport then is made up of a join query using the NameID from the first table.

SELECT *
FROM OtherTable
WHERE OtherID IN 
(SELECT a.otherID 
FROM OtherTable a 
INNER JOIN NameTable b 
ON a.variable = b.variable 
WHERE b.NameID = '111')

I just want a way to make a matching report and subreport for every different NameID in the table. Is there a way to set a parameter or variable that can be then passed to the subreport or any other way for me to do this?

回答1:

In your subreport create a parameter called NameID and use this in your SQL:

SELECT *
FROM OtherTable
WHERE NameID = @NameID

Add this subreport to your table in the main report. Click on the subreport properties and click the Parameters button. This shows the parameters of the linked report and allows you to put expressions of what values you want to pass. To pass the field to the subreport's parameter value, put in something like:

=Fields!NameID.Value