当您从 XML 和节点中删除<data>
包装器时,此代码工作正常,但是当您添加它时,如下所示,我得到 0 个结果。
-- Declare XML variable
DECLARE @data XML;
-- Element-centered XML
SET @data =
N'
<data xmlns="test.xsd">
<subdata>
<customer>
<id>1</id>
<name>Allied Industries</name>
</customer>
<customer>
<id>2</id>
<name>Trades International</name>
</customer>
</subdata>
</data>';
-- Using the query() method
SELECT T.customer.query('id').value('.', 'INT') AS customer_id,
T.customer.query('name').value('.', 'VARCHAR(20)') AS customer_name
FROM @data.nodes('data/subdata/customer') AS T(customer);
但是当我这样做时效果很好:
-- Element-centered XML
SET @data =
N'
<subdata>
<customer>
<id>1</id>
<name>Allied Industries</name>
</customer>
<customer>
<id>2</id>
<name>Trades International</name>
</customer>
</subdata>
';
-- Using the query() method
SELECT T.customer.query('id').value('.', 'INT') AS customer_id,
T.customer.query('name').value('.', 'VARCHAR(20)') AS customer_name
FROM @data.nodes('subdata/customer') AS T(customer);
<data>
当父包装器存在时,有谁知道我如何或为什么在第一个示例中没有得到结果?