An SSRS dataset has two T-SQL queries. One query will only run if a certain parameter has two or more values selected. The other query will only run if the parameter has only one value selected. How can I let the dataset know whether one value or multiple values were selected from the parameter?
-
1You can create an addional integer parameter, set it to be **hidden** and use an expression like this as default value: `Parameter!YourMultipleValueParam.Value.Count` then in the dataset you can use the hidden parameter to determine the number of values selected in `YourMultipleValueParam` parameter. – alejandro zuleta Feb 09 '17 at 19:01
-
Okay, what do I write in the main dataset in order to define which query is to be used? Will something like IF (@YourMultipleValueParam > 1) and (@YourMultipleValueParam = 1) be fine? – K. Ventura Feb 09 '17 at 19:30
-
I mean you have two create an additional hidden parameter in SSRS and use it to pass the number of selected values to the dataset. So if you name the hidden parameter as `NumberOfValues` you should use `IF( @NumberOfValues > 1)` in SQL. – alejandro zuleta Feb 09 '17 at 19:34
-
Okay this works perfectly and it solved my issue, but I can't mark your response as the best answer since for some reason it won't let me but I'll just say that this case is now closed to avoid any future unneeded answers – K. Ventura Feb 09 '17 at 22:35
-
I'll add an answer so you can close the question positively. – alejandro zuleta Feb 09 '17 at 22:37
2 Answers
You can create an addional integer parameter called NumberOfValues
, set it to be hidden
and use an expression like the below in the Default Value:
=Parameter!YourMultipleValueParam.Value.Count
Then in the dataset you can use the NumberOfValues
parameter to determine the number of values selected in YourMultipleValueParam
parameter.
IF @NumberOfValues > 1 BEGIN ....

- 13,962
- 3
- 28
- 48
Good question. Here's one way I've found around this.
Step 1: In your dataset's properties, go to the "Parameters" tab. Add a parameter with the following expression, replacing MyParameterName
with your parameter's name.
=join(Parameters!MyParameterName.Value,",")
Step 2: If you don't already have one, add a splitting function to your SQL Server functions. Simple google search would direct you to something like this: Split function equivalent in T-SQL?
Step 3: Select a count of parameters used in your SSRS report. Based on the result, do what is desired: (My splitting function is simply called Split)
IF (select count(*) from Devl.dbo.Split(@MyParameterName,',')) = 1
BEGIN
/* Do your second query here, when one parameter is selected */
END
IF (select count(*) from Devl.dbo.Split(@MyParameterName,',')) > 1
BEGIN
/* Do your first query here, when more than one parameter is selected */
END
EDIT: Alternatives to steps 2 and 3 if you don't want to (or can't) create/utilize a split function.
Step 2: In your main query at the top, include the following (basically an in-line version of a split function)
declare @string varchar(max), @delimiter char(1)
declare @temptable table (items varchar(max))
set @string = @MyParameterName
set @delimiter = ','
declare @idx int, @slice varchar(max)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
Step 3: Select a count of parameters used in your SSRS report. Based on the result, do what is desired: (My splitting function is simply called Split)
IF (select count(*) from @temptable) = 1
BEGIN
/* Do your second query here, when one parameter is selected */
END
IF (select count(*) from @temptable) > 1
BEGIN
/* Do your first query here, when more than one parameter is selected */
END
-
I understand everything except for the split function. Where do I write the function? Do I add it in the main dataset (the one with two queries), the parameter's dataset, or do I put it in a new dataset? – K. Ventura Feb 09 '17 at 19:31
-
Neither, it's a function (https://msdn.microsoft.com/en-us/library/ms186755.aspx) to be added to the database (think SSMS, not through reporting services). Once it's in the database, you can use it in your queries. If you don't have access to add it to the database, I'll modify step 2 to include an in-line solution that you can simply add to the top of the query. – Matt H Feb 09 '17 at 19:38
-
Although there is nothing wrong with Matt H's solution, I would go for Alejandro's solution as it uses SSRS functions to determine the number of parameter value selected. Once you have added the parameter as Alejandro suggested, in your data set you can simple edit your data set to read IF @MyNewParameter >1 BEGIN ....your multi value query... END ELSE. BEGIN your single value query END. (apologies for lack of line breaks) – Alan Schofield Feb 09 '17 at 19:56
-
I agree with @Canadean_AS. I didn't see alejandro's comment to your question. The method I posted was made for actually utilizing all the parameters selected in the queries in unique ways, rather than simply counting them. Alejandro's is a better solution for your question. – Matt H Feb 09 '17 at 20:04
-
Sorry, I cannot edit the database or add new tables since I only have read-only access to the database. There is a reason I didn't include the actual parameter names and queries in my question since all of that must stay within the company. So yeah, Alejandro's solution was perfect since it did the job just fine, but I can't mark his comment as the best answer so as of now this case is pretty much closed. – K. Ventura Feb 09 '17 at 22:33
-
@K.Ventura He's posted his comment as an answer now, lets up-vote and you should mark it as an answer, so his is on top. I won't be removing my answer, as it may be a good answer for those seeking to use the parameters in their logic rather than the count. But his answer should be above mine for this question. :) – Matt H Feb 10 '17 at 19:01