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

  • The following code (based off of your query) works for me. Note that I build the XML string by hand, but your XML string should look similiar.

    [Code]

    declare @C XML

    set @C = ' '

    select m.item.value('CUSTOMER_NUMBER[1]','integer') [Customer_Number]

    from @C.nodes('/C/CUSTOMER') as m(item)

    [/Code]

    The result set I get back is:

    Customer_Number

    --------------

    1

    2

    There must be a problem with the XML string that you are passing to the @C parameter.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • lklein (5/8/2008)


    This is so frustrating... yikes - I get an error running that just in design view right?

    Yes, the RaisError raises an error. In Preview mode, you'll see the xml line that you're passing to the procedure.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • This is the function in the code section - which is different from the beginning article.

    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 = " "

    For Each sParamItem In MultiValueList

    ReturnString &= " "

    Next

    ReturnString &= " "

    Return (ReturnString)

    End Function

  • lklein (5/8/2008)


    This is the function in the code section - which is different from the beginning article.

    If you're not using the function in the beginning article, I don't know what you're doing. And the post you sent is removing all of the xml tags, so all I see is a string of spaces.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Hi Wayne,

    Nice one, have you looked at the performance of using the XML joins in your stored procedure?

    I did quite a bit of performance testing with the "other" methods [/url] and the results were quite interesting, would love to see how XML performs with this.

  • Adriaan Davel (5/9/2008)


    Hi Wayne,

    Nice one, have you looked at the performance of using the XML joins in your stored procedure?

    In my "casual" testing, XML datasets up to about 1000 "records" performed pretty fast. Beyond 1000 it started slowing down; when it got to 5000 it was crawling, and I stopped one query with > 20,000 elements after about an hour. Most of the XML datasets I use are in dealing with SSRS, where the users want to be able to select > 1 option. Most of these selections are kept down to 15000. but, they've been told...

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Wayne,

    I've put the original code back in the code property and have the same results - nothing returns. However, now that it's back to your code - is there anything on the sql server that needs to be set to allow XML?

    Function ReturnXML(ByVal MultiValueList As Object, ByVal Root As String, ByVal Node As String, ByVal Element As String) As String

    Dim ReturnString = ""

    Dim sParamItem As Object

    ReturnString = " "

    For Each sParamItem In MultiValueList

    ReturnString &= " "

    Next

    ReturnString &= " "

    Return (ReturnString)

    End Function

  • Hello again...

    As you can see, posting XML strings is stripping everything out.

    I've recoded this to build the string in a more complex method. Hopefully, it will post okay.

    This code, when I run it, returns 2 records. What do you get?

    declare @start char(1), @end char(1), @Root char(1), @Node char(8), @Element char(15)

    select @Start = char(60), @end = char(62), @Root = 'C', @Node = 'CUSTOMER', @Element = 'CUSTOMER_NUMBER'

    declare @C XML

    set @C = @Start + @Root + @End +

    @Start + @Node + @End + @Start + @Element + @End + '1' + @Start + '/' + @Element + @End + @Start + '/' + @Node + @End +

    @Start + @Node + @End + @Start + @Element + @End + '2' + @Start + '/' + @Element + @End + @Start + '/' + @Node + @End +

    @Start + '/' + @Root + @End

    select m.item.value('CUSTOMER_NUMBER[1]','integer') [Customer_Number]

    from @C.nodes('/C/CUSTOMER') as m(item)

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Yes - I get 1,2

  • WayneS (5/12/2008)


    Hello again...

    As you can see, posting XML strings is stripping everything out.

    I've recoded this to build the string in a more complex method. Hopefully, it will post okay.

    This code, when I run it, returns 2 records. What do you get?

    declare @start char(1), @end char(1), @Root char(1), @Node char(8), @Element char(15)

    select @Start = char(60), @end = char(62), @Root = 'C', @Node = 'CUSTOMER', @Element = 'CUSTOMER_NUMBER'

    declare @C XML

    set @C = @Start + @Root + @End +

    @Start + @Node + @End + @Start + @Element + @End + '1' + @Start + '/' + @Element + @End + @Start + '/' + @Node + @End +

    @Start + @Node + @End + @Start + @Element + @End + '2' + @Start + '/' + @Element + @End + @Start + '/' + @Node + @End +

    @Start + '/' + @Root + @End

    select m.item.value('CUSTOMER_NUMBER[1]','integer') [Customer_Number]

    from @C.nodes('/C/CUSTOMER') as m(item)

    I get 1 & 2 too.

    But I haven't managed to pass the multi value paramater as an XML to my sproc.

    Did anyone got it to work?

    This is my thread:

    http://www.sqlservercentral.com/Forums/Topic1115348-150-1.aspx#bm1115415

    Thanks.

  • Hi,

    very useful code..

    can u please let me know how to call this custom code function from SSRS ?

  • 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.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 12 posts - 31 through 41 (of 41 total)

You must be logged in to reply to this topic. Login to reply