使用SQL通过回车获取XML节点值

时间:2015-06-25 07:47:41

标签: sql sql-server xml xquery-sql

如何获得' note'的价值标签值带回车符?
这是我的代码:

declare @Input xml='<Root>
  <Addresses>
    <Address>
      <note>AAA</note>
    </Address>
    <Address>
      <note>BBB</note>
    </Address>
    <Address>
      <note>CCC</note>
    </Address>
    <Address>
      <note>DDD</note>
    </Address>    
  </Addresses>
</Root>'

 SELECT CONVERT(nvarchar(max),m.c.query('.//Address/note/text()')) AS Comment   
 FROM @Input.nodes('Root/Addresses') AS m ( c )

我期待的结果如下,而不是'AAABBBCCCDDD'

AAA
BBB
CCC
DDD

1 个答案:

答案 0 :(得分:2)

你可以使用这样的东西来获得理想的结果:

declare @Result nvarchar(max)

select @Result = isnull(@Result + char(13) + char(10), '') + m.c.value('.', 'nvarchar(max)')
from @Input.nodes('Root/Addresses/Address/note') as m(c)

select @Result