日期:2014-05-17 浏览次数:20510 次
create table A表(xmlcontent xml)
insert into A表
select '
<root>
<questions>
<question id="Q1">
<answer>aaa</answer>
<answer>bbb</answer>
</question>
<question id="Q2">
<answer>bbb</answer>
<answer>ddd</answer>
</question>
</questions>
</root>' union all
select '
<root>
<questions>
<question id="Q1">
<answer>aaa</answer>
<answer>ccc</answer>
</question>
<question id="Q2">
<answer>aaa</answer>
<answer>bbb</answer>
</question>
</questions>
</root>'
-- 以字符串形式查询
with t as
(select distinct o.value('.','varchar(10)') 'answer'
from A表
cross apply xmlcontent.nodes('/root/questions/question/answer') x(o)
)
select stuff((select ','+answer from t