2013-07-20 134 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