0

I have the following XML

    <?xml version="1.0"?>
    <ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="id">
        <int>984026</int>
        <int>1005222</int>
    </ArrayOfInt>

I pass this to a store procedure with an XML Parameter.

I want to return all the rows that the id is in the int nodes from xml doc

    SELECT * FROM
    MyTable
    WHERE
    MyTable.Id IN @XMLDoc.value('(.)[1]', 'int')

This only works if I send one int in the xml

    <?xml version="1.0"?>
    <ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="id">
        <int>984026</int>
    </ArrayOfInt>

But if I run this with the two int nodes, sql concatenates the nodes

The conversion of the nvarchar value '9840261005222' overflowed an int column.
1
  • what database are we talking about? Commented Jan 12, 2014 at 23:10

1 Answer 1

1
    declare @XMLDoc xml = '<?xml version="1.0"?>
           <ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="id">
               <int>984026</int>
               <int>1005222</int>
           </ArrayOfInt>'

    ;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as xsi ,
                'id' as ns2,
                DEFAULT 'id')
    SELECT MT.* 
    FROM MyTable
    join @XMLDoc.nodes('/ArrayOfInt/int') T(z) 
    on MT.ID = T.z.value('.','int')
Sign up to request clarification or add additional context in comments.

2 Comments

Doesnt works for me, I get no results, the only way I get any row as a result is by changing "join @XMLDoc.nodes('/ArrayOfInt/int') T(z)" for "join @XMLDoc.nodes('.') T(z)" but it only works when I have one int node in the xml. When I have two nodes or more, sql concatenates them and keeps throwing "The conversion of the nvarchar value '9840261005222' overflowed an int column." By the way, I didnt put the "WITH XMLNAMESPACES" as I don't declare the xml, I get it as a parameter.
Getting rid of 'xmlns="id"' solved the problem using your answer, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.