In SSRS reports we can have multi valued paremeters, which means we can select multiple values for that parameter and based on all those selected values result in the report is shown. We can write the SQL query to pass those parameter’s values and populate the data set, but sometimes it is required or to optimize the performance we need to use the stored procedure instead of direct sql query. This stored procedure will be taking the report paremeter as input and returing the result set to populate the data set.
In case of sql query as well as stored procedure, all selected values of the multi-value parameter will be passed as ‘,’ (comma) separated. So we should use IN clause in WHERE to impose the desired condition.
Problem: If we use stored procedure passing an input parameter which is multi-value parameter, SSRS will take all those selected multiple values as different-2 parameters (as they all are ‘,’ separated). And thus if we do multiple selection (or SelectAll) error would be there, though it will work fine if we select only one value.
error(s): Must Declare Scalar variable @parameter OR number of parameters being passed are more than expected
Example: We will specify the ‘Multi-Value’ option while creating a report paremeter in SSRS reports. Lets take a report which displays the products information based on the selected colors of the product (Multiple selection). For that lets create a table report which uses a data source to AdventureWorks database. AdventureWorks is the DataSet being used to execute a stored procedure & to get the desired data based on @Color parameter.
EXEC GETProducts @Color
GetProducts is stored procedure which uses @Color parameter in the WHERE clause.
WHERE @Color IS NULL OR Color IN (@Color)
Now we create a dataset (dsColor) for populating the drop down list of the color parameter.
SELECT DISTINCT Color FROM Production.Product
Now we will define the @Color parameter:
Now if preview the report it will work fine for single value of color, but if we select multiple colors from colors parameters, it will give error. To pass the complete list of selected values of @color parameter as a single value/string we should use a function JOIN().
In data set properties of AdventureWorks dataset, click on parameters tab and use JOIN() funtion as:
This function will join all the color values and will a give a comma separated values thus a single value as a whole will be passed to the SP. We can use any other de-limiter other than ‘,’ if our values already contain ‘,’ .
To use this list of comma separated values in the stored procedure we need to split them in a proper way as they are as : ‘value1, value2, value3’, but we need them as ‘value1’, ‘value2’, ‘value3’ to use them in IN clause. So we need to have a UDF function which will take this string of values and will return all them splitted. And then use them in SP as:
WHERE @Color IS NULL OR Color IN (SELECT * FROM SplitList(@Color, ‘,’))
where SplitList is a UDF which takes the list and the de-limiter as input parameters and splits all the values.