Sort XML Attributes in SQL

2019-09-01 04:02发布

问题:

How to sort XML Attributes in SQL?

for example for this XML:

<books><book b='' c='' a=''/></books>

I want:

<books><book a='' b='' c=''/></books>

回答1:

From http://msdn.microsoft.com/en-us/library/ms187107%28v=sql.90%29.aspx:

The order of the XML attributes is not preserved. When you query the XML instance stored in the xml type column, the order of attributes in the resulting XML may be different from the original XML instance.

So even if you could figure out a way of sorting the attributes, you can not trust that the XML data type in SQL Server will preserve the order you want.