strange behavior of SQL Server when sum nodes's values in XML

后端 未结 4 1599
后悔当初
后悔当初 2021-01-20 18:11

I ask a question about sum node\'s values:

sum some xml nodes values in sql server 2008

Please consider this code:

Declare @xml xml 
set @xml         


        
相关标签:
4条回答
  • 2021-01-20 18:46

    Try this is working with BIGINT:

    DECLARE @SearchKeyWords XML 
    
    SET @SearchKeyWords =
    '<Parent ID=''p''>
        <Child ID="1">1000000000</Child > 
        <Child ID="2">234650</Child > 
        <Child ID="3">0</Child > 
    </Parent >' 
    
    DECLARE @TempSearchKeyWords TABLE
            (
                SearchKeyWord BIGINT
            )
    
    INSERT INTO @TempSearchKeyWords                     
    SELECT SearchKeyWords.SearchKeyword.value('.',' bigint ') AS SearchKeyword             
    FROM @SearchKeyWords.nodes('/Parent/Child') AS SearchKeyWords(SearchKeyword)
    
    SELECT SUM(SearchKeyWord) FROM @TempSearchKeyWords
    
    0 讨论(0)
  • 2021-01-20 18:58

    The XML you have here is untyped XML. That means the values provided to sum() is of type xdt:untypedAtomic. When sum() is used on xdt:untypedAtomic the values are cast to xs:double. The result of sum() is read by the values() function as a string (or untypedAtomic) and xs:double uses the scientific notation when the value is less than 1.0E-6, or greater than or equal to 1.0E6. SQL Server can not convert from scientific notation to int or bigint.

    Ref:
    sum Function (XQuery)
    Type Casting Rules in XQuery

    Other answers have provided workarounds that cast the input values or the result from sum() or using float as the data type in values(). Another option could be to use typed XML instead.

    Schema:

    create xml schema collection XSDSumTest as '
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Parent">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Child" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:simpleContent>
                                <xs:extension base="xs:int">
                                    <xs:attribute name="ID" type="xs:int"/>
                                </xs:extension>
                            </xs:simpleContent>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="ID" type="xs:string"/>
            </xs:complexType>
        </xs:element>
    </xs:schema>'
    

    Query:

    declare @xml xml(XSDSumTest) = '
    <Parent ID="p">
      <Child ID="1">1000000000</Child > 
      <Child ID="2">234650</Child > 
      <Child ID="3">0</Child > 
    </Parent>'
    
    select @xml.value('sum(/Parent[@ID="p"]/Child)','bigint') as Sum
    
    0 讨论(0)
  • 2021-01-20 19:01

    Try this one -

    DECLARE @xml XML 
    SELECT @xml='<Parent ID="p">
         <Child ID="1">1000000000</Child > 
         <Child ID="2">234650</Child > 
         <Child ID="3">0</Child > 
          </Parent >'
    
    SELECT @xml.value('sum(for $r in /Parent[@ID="p"]/Child return xs:int($r))', 'bigint')
    

    UPDATE:

    DECLARE @xml XML 
    SELECT @xml='<Parent ID="p">
         <Child ID="1">100000000000000</Child > 
         <Child ID="2">234650</Child > 
         <Child ID="3">0</Child > 
          </Parent >'
    
    SELECT @xml.value('sum(for $r in /Parent[@ID="p"]/Child return xs:decimal($r))', 'bigint')
    

    UPDATE 2:

    DECLARE @xml XML 
    SELECT @xml='<Parent ID="p">
         <Child ID="1">100000000000000.6</Child > 
         <Child ID="2">234650</Child > 
         <Child ID="3">0</Child > 
          </Parent >'
    
    SELECT @xml.value('sum(for $r in /Parent[@ID="p"]/Child return xs:decimal($r))', 'decimal(18,2)')
    
    0 讨论(0)
  • 2021-01-20 19:05

    Sql Server has a problem converting the value with scientific notation from a string to an integer, as would happen when you run your xpath query, however, it can do this for float.

    You could write your query like this:

    select @xml.value('sum(/Parent[@ID = "p"]/Child) cast as xs:long?', 'bigint')
    
    0 讨论(0)
提交回复
热议问题