Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
545 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Using a Parameter to set the Data Source for a Report |
---|
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. |
In this blog
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:
SELECT
StudioId AS IdField,
StudioName AS NameField
FROM
tblStudio
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!
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.