views:

59

answers:

1

Hi,

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.

A: 

How about an XQuery expression;

select id, data
from T
  order by data.value('(/Data/SimpleOrderedValue)[1]', 'int') 
Alex K.
Thanks Alex, will it use indexes if I use this expression in where clause?
Kirill Muzykov
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.
Alex K.