Phone (01457) 858877 or email
A single report in Reporting Services can serve many masters. This blog shows how to allow a user to choose which data a report should display, using a dropdown parameter.
How cool would this be?
When you run the report, you can either show a list of film directors of a list of film studios.
|If you choose to show directors||If you choose to show studios|
It turns out that this sort of one-report-serving-all effect is remarkably easy to achieve. There are 3 stages: creating the drop-down parameter, creating the dataset for the report and creating the table.
The first thing to do is to make sure that the dropdown parameter appears at the top of your report:
First add your parameter (this assumes you're using SSRS 2008 or 2012).
It's a text parameter, which we've called ReportTable (so far, so straightforward):
Give your parameter a name, and a sensible prompt.
For the list of available values to comprise the drop list, make the visible label something easy to read, but use the underlying table name for the value:
Add one possible available value for each table a user can choose.
The trick to get our report working is to make sure that whether we choose directors or studios, we still have the same field names. First create a dataset:
When creating the dataset, click on the expression builder as shown.
In the expression builder, type in an expression which will evaluate to a valid SQL statement:
This will always return two fields, called IdField and NameField.
Suppose that the user chooses to show Studios. Then the dataset expression will evaluate to:
StudioId AS IdField,
StudioName AS NameField
This gives the following dataset:
It doesn't matter what parameter value the user chooses, the fields in the dataset will always be the same.
We can now create a table for the report:
A standard table, displaying two columns.
The expression for the title of the report could be something like:
The title changes according to whether directors or studios are being reported.
Although the above is relatively easy to implement, it may run slowly (dynamic SQL can't be optimised). Perhaps a better idea would have been to create two stored procedures, and dynamically allocate these to the report dataset:
You can use an expression for the stored procedure, which would depend on the parameter value chosen.
Just a thought to finish up on!
Comments on this blog
This blog has one comment:
You couldn't show records from two different tables in the same tablix item. You could easily configure a multivalue parameter (let's call it prmTable, say) to allow a user to choose mutliple answers, but what then?
You could use Parameters!prmTable.Value(0) to get at the first value chosen, for example. I suppose you could then have two separate report items, each one displaying different data, but I can't see why you'd want to do this!