简体   繁体   English

如何在Sql Server 2008中检索Xml节点的多个值

[英]How to retrieve multiple values of a Xml node in Sql Server 2008

Look this Xml 看看这个Xml

<Criteres>
   <TypesOffre>
      <TypeOffre>Appartement</TypeOffre>
      <TypeOffre>Maison</TypeOffre>
   </TypesOffre>
</Criteres>

I would like to retrieve the values of TypeOffre node and use them in a JOIN like the following statement 我想检索TypeOffre节点的值,并在JOIN中使用它们,如下面的语句

SELECT T2.Nom
FROM RechercheOffre T1
     INNER JOIN TypeOffre T2 ON (T1.Xml.value('(/Criteres/TypesOffre/TypeOffre)[1]', 'varchar(max)') = T2.Nom)

I can't use the value method because she needs a single instance ... 我不能使用value方法,因为她需要一个实例...

and the query method returns the values of the Typeoffre node in a single column like this 并且查询方法像这样在单个列中返回Typeoffre节点的值

AppartementMaison

Any idea ? 任何想法 ?

Try this: 尝试这个:

SELECT T2.Nom
FROM RechercheOffre T1
CROSS APPLY T1.Xml.nodes('/Criteres/TypesOffre/TypeOffre') AS CTT(TypeOffre)
WHERE 
  T2.Nom = TypeOffre.value('(.)', 'varchar(50)') 

You need to use CROSS APPLY on the .nodes() XQuery function to get a pseudo list of rows of XML, and then you can pull out the individual values from the XML nodes and compare to other bits of your database. 您需要在.nodes() XQuery函数上使用CROSS APPLY来获取XML行的伪列表,然后您可以从XML节点中提取单个值并与数据库的其他位进行比较。

With a JOIN 加入

SELECT T2.Nom
FROM RechercheOffre T1
CROSS APPLY T1.Xml.nodes('/Criteres/TypesOffre/TypeOffre') AS CTT(TypeOffre)
INNER JOIN TypeOffre T2 ON (TypeOffre.value('(.)', 'varchar(50)') = T2.Nom)

Thank's marc_s 谢谢你的marc_s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM