如何在tsql中查询xml列 [英] How to query xml column in tsql

查看:36
本文介绍了如何在tsql中查询xml列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 2008 上有一个表 T1,其中有一个 XML 列 EventXML.我想查询某个节点包含特定值的所有行.更好的是,我想检索不同节点中的值.表T1:

I have a table, T1, with a XML column, EventXML, on SQL Server 2008. I want to query all the rows where certain node contains a particular value. Better, I'd like to retrieve the value in a different node. The table T1:

T1:
   EventID, int
   EventTime, datetime
   EventXML, XML

这是一个示例 XML 层次结构:

Here is an example XML hierarchy:

<Event>
   <Indicator>
      <Name>GDP</Name>
   </Indicator>
   <Announcement>
      <Value>2.0</Value>
      <Date>2012-01-01</Date>
   </Announcement>
</Event>

  1. 如何查找与GDP"指标相关的所有行;
  2. 如何获取GDP"指标的所有值;

推荐答案

这个怎么样?

SELECT 
    EventID, EventTime,
    AnnouncementValue = t1.EventXML.value('(/Event/Announcement/Value)[1]', 'decimal(10,2)'),
    AnnouncementDate = t1.EventXML.value('(/Event/Announcement/Date)[1]', 'date')
FROM
    dbo.T1
WHERE
    t1.EventXML.exist('/Event/Indicator/Name[text() = "GDP"]') = 1

它将查找 /Event/Indicator/Name 等于 GDP 的所有行,然后将显示 // 用于这些行.

It will find all rows where the /Event/Indicator/Name equals GDP and then it will display the <Announcement>/<Value> and <Announcement>/<Date> for those rows.

参见 SQLFiddle 演示

这篇关于如何在tsql中查询xml列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆