3

I was wondering can I ORDER BY my query by node of an XML typed column?

For example I have a table

ID (int) | Data (XML)

Where Data column stores XML in form similar to this

<?xml?>
<Data>
   <SimpleOrderedValue>1</SimpleOrderedValue>
   <ComplicatedInternals>
      ...
   </ComplicatedInternals>
</Data>

I want query this table ordered by SimpleOrderedValue. Can I do this on MS SQL Server 2008 with XML column? Or can I do thins using CLR UDT, but without additional computed column and in a way so that Data column were indexed (for faster search).

Will appreciate any help. Thanks.

1 Answer 1

4

How about an XQuery expression;

select id, data
from T
  order by data.value('(/Data/SimpleOrderedValue)[1]', 'int') 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Alex, will it use indexes if I use this expression in where clause?
It should do as the XML is only parsed in the ORDER BY, so it should SELECT using an index then just sort the result, if you look at the query plan you will be able to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.