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

后端 未结 4 1611
后悔当初
后悔当初 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 19:01

    Try this one -

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

    UPDATE:

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

    UPDATE 2:

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

提交回复
热议问题