Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Using XML to pass Multi-Select parameters from SSRS to SQL Server

By Wayne Sheffield,

When passing Multi-Select parameters from Reporting Services to SQL Server, the only way normally mentioned is to pass a comma-delimited string (built with the JOIN() VB function in SSRS) to SQL Server. However, you cannot use this parameter directly in a query, and must either use dynamic sql or utilize a process for converting this string into separate entries into a temp table.

An alternative is to pass the Multi-Select parameter as an XML string.

The format of the XML string that we need to pass to SQL is in this format:
<ELEMENT>element data</ELEMENT>

If we have a multi-select parameter of months (where the month name was displayed, and the month number is the value), the XML for the selected months (of January and July) would look like:

Now, building an XML string can be a bit of hand coding of strings and values. Here is the VB function that I utilize; you can either use it in the Code tab of the report properties, or you can build it into a library and link the dll into the report from the References tab of the report properties.

Function ReturnXML(ByVal MultiValueList As Object, ByVal Root As String, ByVal Node As String, ByVal Element As String) As String
' Returns an XML string by using the specified values.
' Parameters:
' MultiValueList - a multi value list from SSRS
' Root, Node, Element - String to use in building the XML string
Dim ReturnString = ""
Dim sParamItem As Object
ReturnString = "<" & Root & ">" For Each sParamItem In MultiValueList ReturnString &= "<" & Node & "><" & Element & ">" & Replace(Replace(sParamItem,"&","&amp;"),"<", "&lt;") & "</" & Element & "></" & Node & ">" Next
ReturnString &= "</" & Root & ">"
Return (ReturnString)
End Function

So, for the above example, I would build the XML string by the statement:
ReturnXML(Parameters!MultiValue.Value, "Months", "Month", "MonthNumber")

Now, we need to use this in SQL. Since I always use stored procedures to access the data, I'll use this as an example.

In the parameters section of the stored procedure, add a new parameter of the XML datatype, ie: @Months XML

In your selection criteria, add a join condition.

Select (column list)
From (table/view to retrieve data from) a
INNER JOIN @Months.nodes('/Months/Month') AS m(item)
ON month(a.DateField) = m.item.value('MonthNumber[1]', 'integer')

To add this procedure to your report, go to the data tab and select <New Dataset> from the combobox. Set the command type to Stored Procedure, and enter the procedure name, and click on Ok. Then edit this dataset. On the Parameters tab, change the expression for this parameter to "=Code.ReturnXML(Parameters!MultiValue.Value, "Months", "Month", "MonthNumber") and click Ok, where "MultiValue" is the name of the multi-value parameter to use. Note that if this is an existing multi-value parameter, you may have to delete the "[0]" that is added by clicking on the parameter in the expression builder.

Congratulations. You're now passing the multi-value parameter to your SQL Server stored procedure as an XML dataset to your stored procedure, and retrieving the data for the options selected from the report parameter.

Total article views: 8305 | Views in the last 30 days: 7
Related Articles

Passing multi-valued parameter selections in to stored procedures

Problems with passing multi-valued parameter selections as a csv string in to stored procedure param...


Parameter selection causing screen refresh

Parameter selection causing screen refresh


same string selection

same string selection


Generating combinations 'm elements from n-element set'

This procedure generates a dataset with combinations of elements_to_select taken from number_of_valu...


grouping based on parameter selected

grouping based on parameter selected