开发者

Retrieving a unique dataset from XML fields in SQL 2008 R2

开发者 https://www.devze.com 2023-03-04 21:22 出处:网络
I currently have several rows (say around 100 for args sake) in a table, and within that table I have an XML field which contains data as follows (example):

I currently have several rows (say around 100 for args sake) in a table, and within that table I have an XML field which contains data as follows (example):

<cf1>summer</cf1>

.. I might then have another row or rows containing:

<cf1>winter</cf1>

.. and for completeness, another few rows perhaps containing multiples of:

<cf开发者_C百科1>spring</cf1>

So my question is:

How to write a query/proc to return me a unique resultset of all possible xml nodes in my xml field?

I guess I can return say 100 rows, and then using C# to filter that down.. but I am guessing that with SQL 2008 there are far better ways of doing that!


The idea is that you use XQuery to grab the data in a subquery and just treat it as another row in the table. Like such:

SELECT DISTINCT Season
FROM
  (SELECT CAST(Xml_Field.query('data(/cf1)') AS VARCHAR) AS Season
   FROM My_Xml_Table)

This query will return:

Season
------
summer
winter
spring
0

精彩评论

暂无评论...
验证码 换一张
取 消