Note, before getting started with this lesson there are some prerequisites you should know about. Please read my post Getting Started with SQL Server 2008 to ensure you have everything setup correctly, otherwise you will be missing objects required to get the code here to work correctly.
The previous lesson showed how to include a subreport into another report. This could be used to link independent reports together into a single report. It can also be useful to have a related subreport. A subreport whose data is driven by that of the main report. This can be accomplished by the use of paramenters.
For this lab we’ll create a subreport that returns category totals for the region passed in from the main report. Note that this is a greatly simplified example to illustrate the technique. Even though in this sample everything comes from the same database, each report could just as easily come from completely different data sources. Subreports would be a great technique for combining data from different systems.
Step 1. Create the subreport.
Use Contoso as the shared datasource. For the query, enter:
SELECT [Region] , [ProductCategoryName] , SUM([TotalAmount]) AS ProductTotal FROM [ContosoRetailDW].[Report].[V_SubcategoryRegionTotalsByYear] WHERE [Region] = @pRegion GROUP BY [Region], [ProductCategoryName] ORDER BY [Region], [ProductCategoryName]
Use a tabular report, move everything into the details area, Generic for the table style, and for the report name use “Subreport – Region Category Totals”.
Step 2. Cleanup the subreport.
Click the edge Region texbox in the header (so it’s selected instead of being edited), and press delete. Repeat with the [Region] textbox in the detail row. We won’t need it since Region will be displayed on the parent report.
Change the other headers to Category and Total. Make them wider, make what had been the Region column smaller but leave it, it will give a nice indent padding when included on the parent report. In the textbox properties for ProductTotal, make sure to set the Number to Currency, and in the Alignment area change the Horizontal to right align.
Remove the “Subreport – Region Category Totals” text box
Click on the main table grid, then move it to top of body. Collapse the body to fit the table.
Step 3. Add the parameter.
In the Report Data window, right click on Parameters and pick Add Parameter. Name the property Region. For the prompt, enter “Region – Hidden”. Since the prompt will never be visible, it really doesn’t matter, but making a habit of entering the name and the word Hidden will give a clear indicator that this parameter is a hidden one.
Leave the data type set to text, and check on “Allow blank value”. If you don’t, the report will error out when used as a subreport. Next, set the visibility to Hidden. This means it won’t appear if you run the report, but you can still pass in parameters, from another report or via a URL. Click OK to close the properties window.
Finally, we need to bind the parameter to the parameter the dataset needs. Right click on the dataset and go to properties. On the parameters area @pRegion should already be present (remember, it was part of the WHERE clause in the SQL query). Pick @Region in the drop down for Parameter Value.
Step 4. Create the main report.
Add a new report, using Contoso as the shared datasource. For the query, use:
SELECT [RegionCountryName] FROM [Report].[V_Regions]
Use a tabular report, move the RegionCountryName to the details area, and pick Corporate for the style. Finally, for the name use “Regional Report”.
Step 5. Layout the report.
Since there’s only one column, expand it to take up the width of the body.
Right click on the row selector (the gray box with the lines on the left of the table) and pick Insert Row->Inside Group Below.
Into that area, drag a Subreport control from the toolbox. Note in this case there is only one column, but if there were multiple cells you could highlight them, right click and pick Merge Cells.
Step 6. Setup the subreport.
Right click on the subreport control.
Under “Use this report as a subreport” select the “Subreport – Region Category Totals”. Under the parameters area, click Add. Select Region under Name, and for the Value select RegionCountryName.
Step 7. Preview the report
Preview the report to see your results:
Just a few notes. In this report, we left the table headers in the subreport (Category and Total). Often these are removed, to make the subreport blend in more with the parent.
Here only one parameter was passed, however you can pass multiple parameters if you need to.