0

我试图从 XML 数据中获取结果,但只获取第一个节点的值。

create table #temp(xmlString nvarchar(max))
insert into #temp (xmlString) values 
('<?xml version="1.0" ?><response status = "ERROR">
<error>Error1</error>
<error>Error2</error>
</response>')

我想要一个结果:

Error1, Error2

请帮忙。谢谢

4

2 回答 2

3
select
    x.c.value('.', 'nvarchar(128)') as value
from (select cast(xmlString as xml) as data from temp) as t
    outer apply t.data.nodes('/response/error') as x(c)

SQL 提琴示例

于 2013-07-20T21:38:24.347 回答
0

正确答案

select STUFF((select ',' +  x.c.value('.', 'nvarchar(max)') 
from (select cast(xmlString as xml) as data from #temp)
as t outer apply t.data.nodes('/response/error')
as x(c)for xml path('')), 1, 1, '') as Errors
于 2013-07-20T22:31:38.430 回答