【问题标题】:XML Query within SQL ServerSQL Server 中的 XML 查询
【发布时间】:2023-03-16 08:23:01
【问题描述】:

我刚开始在 SQL Server 数据库中查询 XML。我在最基本的查询方面遇到了麻烦。这是一个简化的例子。如何返回描述?下面的 SELECT 语句是我正在使用的,但它什么也不返回。

SELECT Incidents.IncidentXML.query
('data(/dsIncident/IncidentInformation/Description)') AS Description 
FROM Incidents

这是我正在使用的 XML 文件的 sn-p:

<dsIncident xmlns="http://tempuri.org/dsIncident.xsd">
  <IncidentInformation>
    <Description>This is the description.</Description>
    <Country>Singapore</Country>
  </IncidentInformation>
</dsIncident>

【问题讨论】:

    标签: sql sql-server xml xquery


    【解决方案1】:

    好吧,您错过了 XML 命名空间! :-)

    试试这个:

    SELECT 
      Incidents.IncidentXML.query('declare namespace x="http://tempuri.org/dsIncident.xsd";
              (/x:dsIncident/x:IncidentInformation/x:Description)') AS Description 
    FROM Incidents
    

    神奇的是

    declare namespace x="http://tempuri.org/dsIncident.xsd"
    

    part here - 它声明一个命名空间(带有您选择的前缀 - 可以是任何东西 - 这里是“x”),用于查询该 XML 数据的时间段。

    希望这会有所回报! ;-)

    马克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 2013-08-18
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多