Reputation: 3773
I created a SSRS report with a dropdown to parameterize data being reported. This works fine. I now need to add another parameter to filter the report a little further. For example, I have a location dropdown that shows area, country, region, etc. I need to add another dropdown that is dependant on the first dropdown. So if I select "country" in the 1st dropdown, I show the list of countries in the 2nd dropdown or if I select Region - I show list of Regions in the 2nd dropdown. Country, Area, Region data is stored in different tables. So basically my query needs to be smart enough to run the appropriate sql based on 1st dropdown selection.
Thanks so much for any assistance given.
Upvotes: 2
Views: 12384
Reputation: 441
It is very simple. Let’s assume that the data of the table “ONE” as follows:
**Location_Type** **Location** Country India Country Sri Lanka Country China Country Japan City Bangalore City Hyderabad City Delhi
Ex:- Query for the Report parameter (@Location_Type1):
Select Distinct Location_Type from One
Query for the second Report Parameter:
Select Location from ONE where Location_Type = @Location_Type1.
Please let me know if it is not clear.
Upvotes: 1
Reputation: 20560
One of the powerful features of Reporting Services is that everything is an expression, including the dataset's SQL statement.
Let's say your first parameter (the one that describes what to select) is called Location
and it selects a list of locations such as country, region, etc. Perhaps you get that from a table which has a LocationId
and a Description
like so:
SELECT LocationId, Description FROM Locations
You hook up the Location parameter to this query to get your drop down list of location selectors for the Location
parameter.
Now create a second parameter called Select
where we want to select from a list of countries or regions. To keep it simple, I'm going to assume there are only two locations: Country with a LocationId of 1 and Region with a LocationId of 2.
Create a new dataset called Selections
and manually add fields to it called Id
and Description
. Hook up your Select
parameter to this dataset. Now for the SQL Statement for the Selections
dataset, enter the following expression:
=IIF(Parameters!Location.Value = 1,
"SELECT CountryId AS Id, CountryName AS Description FROM Countries",
"SELECT RegionId AS Id, RegionName AS Description FROM Regions")
So, where the Location
parameter is set to 1 (Country) you select from the Countries table otherwise you select from the Regions table. You alias the field names so you get consistently named fields for your dataset for use in the Select
parameter query. Obviously, you can extend this to more selections as required.
You get the idea but this is a little fragile - whenever you want to add a new location type you have to go through all your reports and update the SQL statement for the Selections
dataset. That's tedious and no one wants that job. What we want is an automated system where all reports get the new selections whenever they are added.
So let's add a column to the Locations table, called SQLStatement. For the Country row, this will have the value:
SELECT CountryId AS Id, CountryName AS Description FROM Countries
For the Region row, the SQLStatement field has the value:
SELECT RegionId AS Id, RegionName AS Description FROM Regions
Now the Locations table has the value of the SQLStatement in it for the Selections
dataset. You can't use this directly (your dataset would just return the value of the SQL statement field, not execute it) but you want to have something that returns this string as the expression to use for the SQL statement for the Selections
dataset. Custom code can be used to do this. So the expression to use for the Selections
dataset will be something like this:
=Code.GetSQLStatement(Parameters!Location.Value)
And then you have custom code function like this (significant parts left out):
Public Function GetSQLStatement(LocationId As Integer) As String
Dim SQL As String
SQL = "SELECT SQLStatement FROM Locations WHERE LocationId = " + CStr(LocationId)
' Connect to database, get SQLStatement field
GetSQLStatement = <Field Value>
End Function
When you want to add another location selection, for example continents, all you have to do is add another row to the Locations table, say LocationId = 3, Description = Continent and SQLStatement = SELECT ContinentId AS Id, ContinentName AS Description FROM Continents
and now every report you have that selects by location will be able to use Continents.
Upvotes: 5
Reputation: 3469
First dataset add this SQL
Select ContryName, CountryID From Country
Assuming the name the above dataset's parameter is @country add the following SQL on the second dataset
Select RegionName, RegionID From Region
Where CountryID IN( @country)
Upvotes: 1