我有一个存储过程接收一些这样的XML:
<Root>
<pickh attribute1="897" attribute2="GGG" ....>
<pickd attribute1="123" attribute2="678" ..../>
</pickh>
</Root>
或
<Root>
<rcpth attribute1="ABC" attribute2 ="DEF" ....>
<rcptd attribute1="012" attribute2="345" ..../>
</rcpth>
</Root>
我需要做的是:
select Nodetype = (here is the part that I need help),
Attribute1 = nodes.c.value('@Attribute1','varchar(40)'),
Attribute2 = nodes.c.value('@Attribute2','varchar(40)')
from @Xml.nodes('/Root//*') as node(c)
此查询应返回结果:
NodeType Attribute1 Attribute2
pickh 897 GGG
pickd 123 678
rcpth ABC DEF
rpctd 012 345
有办法做到这一点吗?
答案 0 :(得分:2)
declare @Xml xml
set @Xml = '<Root>
<pickh attribute1="897" attribute2="GGG" >
<pickd attribute1="123" attribute2="678" />
</pickh>
</Root>'
select NodeType = node.c.value('local-name(.)', 'varchar(15)'),
Attribute1 = node.c.value('@attribute1','varchar(40)'),
Attribute2 = node.c.value('@attribute2','varchar(40)')
from @Xml.nodes('/Root//*') as node(c)
答案 1 :(得分:0)
是的,这非常容易:
select Nodetype = node.c.value('local-name(.)','varchar(40)'),
Attribute1 = node.c.value('@attribute1','varchar(40)'),
Attribute2 = node.c.value('@attribute2','varchar(40)')
from @Xml.nodes('/Root//*') as node(c)