我需要从 XML 文档中选择值。这些值存储为子节点,如下所示:
<customers>
<customer>
<kunnr>1</kunnr>
<kdgrp>2</kdgrp>
</customer>
<customer>
<kunnr>2</kunnr>
<kdgrp>2</kdgrp>
</customer>
</customers>
我需要为每个客户节点选择 kunnr 和 kdgrp 的值。我期待这样的结果:
kunnr kdgrp
1 2
2 2
到目前为止我尝试了什么:
SELECT @xml.query('/customers/customer/kunnr') AS KUNNR,
@xml.query('/customers/customer/kdgrp') AS KDGRP
这会导致一行包含两个包含 XML 的列:
KUNNR KDGRP
<kunnr>1</kunnr><kunnr>2</kunnr> <kdgrp>2</kdgrp><kdgrp>2</kdgrp>
另一个尝试:
SELECT C.value('/kunnr/text()','nvarchar(10)') as KUNNR,
C.value('/kdgrp/text()','nvarchar(10)') as KDGRP
from @xml.nodes('/customers/customer') AS T(C);
这导致以下错误消息:
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'