-- Declare XML variable
DECLARE @data XML;
-- Element-centered XML
SET @data =
N'<data>
<customer>
<id>1</id>
<name>Allied Industries</name>
</customer>
<customer>
<id>2</id>
<name>Trades International</name>
</customer>
</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/customer') AS T(customer);
-- Using the value() method
SELECT T.customer.value('(id)[1]', 'INT') AS customer_id,
T.customer.value('(name)[1]', 'VARCHAR(20)') AS customer_name
FROM @data.nodes('data/customer') AS T(customer);
-- Select only customer which id equals 2 using the exist() method
SELECT T.customer.value('(id)[1]', 'INT') AS customer_id,
T.customer.value('(name)[1]', 'VARCHAR(20)') AS customer_name
FROM @data.nodes('data/customer') AS T(customer)
WHERE T.customer.exist('id/text()[. = "2"]') = 1;
-- Attribute-centered XML
SET @data =
N'<data>
<customer id="1" name="Allied Industries"/>
<customer id="2" name="Trades International"/>
</data>';
-- Using the value() method
SELECT T.customer.value('@id', 'INT') AS customer_id,
T.customer.value('@name', 'VARCHAR(20)') AS customer_name
FROM @data.nodes('data/customer') AS T(customer);
-- Results
customer_id customer_name
----------- --------------------
1 Allied Industries
2 Trades International
Testing the processing for element-centered XML versus attribute-centered XML does show that attribute-centered XML is processed faster.
Resources:
XML Support in Microsoft SQL Server 2005
http://msdn2.microsoft.com/en-us/library/ms345117.aspx

3 comments:
This (attribute based) select statement works also.
Any signficant difference?
I stumbled upon it, converting a element based statement to attribute based.
SELECT T.customer.value('@id[1]', 'INT') AS customer_id, T.customer.value('@name[1]', 'VARCHAR(20)') AS customer_name FROM @data.nodes('data/customer') AS T(customer);
Don't forget about this Sql Server 2005 (SP2) issue.
It seems to still be an issue in 2008, despite "Tomer"'s remarks to the contrary.
This (attribute based) select statement works also.
Any signficant difference?
I stumbled upon it, converting a element based statement to attribute based.
SELECT T.customer.value('@id[1]', 'INT') AS customer_id, T.customer.value('@name[1]', 'VARCHAR(20)') AS customer_name FROM @data.nodes('data/customer') AS T(customer);
Don't forget about this Sql Server 2005 (SP2) issue.
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=250407
It seems to still be an issue in 2008, despite "Tomer"'s remarks to the contrary.
Hi shh,
Although your query produces the same results, it is not technically equivalent path expression to the one I posted. The query that you posted uses the position predicate [1] to pull the first occurrence. However, in attribute-centered XML you cannot have duplicate attributes, so essentially the results will be the same.
Yes, I am aware of the feedback item posted by Erland Sommarskog and voted long time ago. Hope we see that resolved.
Post a Comment