SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

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

By Wayne Sheffield, 2008/05/08

Total article views: 5829 | Views in the last 30 days: 118

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:
<ROOT>
<NODE>
<ELEMENT>element data</ELEMENT>
</NODE>
</ROOT>

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:
<Months>
<Month>
<MonthNumber>1</MonthNumber>
</Month>
<Month>
<MonthNumber>7</MonthNumber>
</Month>
</Months>

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.

By Wayne Sheffield, 2008/05/08

Total article views: 5829 | Views in the last 30 days: 118
Your response
 
 
Related tags

Reporting Services    
XML    
 
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com