This code works great when removing a shell <data>from XML and nodes, but when you add it as shown below, I get 0 results.
DECLARE @data 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>';
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);
But it works fine when I do it like this:
SET @data =
N'
<subdata>
<customer>
<id>1</id>
<name>Allied Industries</name>
</customer>
<customer>
<id>2</id>
<name>Trades International</name>
</customer>
</subdata>
';
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);
Does anyone know how and why I do not get the results in the first example when the parent shell exists <data>?
source
share