日期:2014-05-17 浏览次数:20498 次
declare @t table(col xml)
insert @t select '<root>
<item>
<name>A</name>
<type> 1</type>
<attr>A</attr>
<field> B</field>
</item >
<item>
<name>B</name>
<type> 2</type>
<attr>C</attr>
<field>D</field>
</item>
</root>'
select
b.c.value('(name/text())[1]','varchar(50)')name
,b.c.value('(type/text())[1]','varchar(50)')type
,case when b.c.exist('type[text()=1]')=1 then b.c.value('(attr/text())[1]','varchar(50)') end attr
,case when b.c.exist('type[text()=2]')=1 then b.c.value('(field/text())[1]','varchar(50)') end field
from @t as a
cross apply a.col.nodes('root/item') as b(c)
/*
name type attr field
A 1 A NULL
B 2 NULL D
*/