2013-07-20 102 views
0

我想从XML数据中获取结果,但只获取第一个节点的值。从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 

请帮助。 感谢

+0

你试图解决这个问题是如何结束? – OzrenTkalcecKrznaric

回答

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 FIDDLE EXAMPLE

+0

感谢罗马人,我得到了你想要的答案,再次感谢 – Maddy

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