简体   繁体   English

SQL Server中的XML查询

[英]XML Query within SQL Server

I just starting to query XML within a SQL Server database. 我刚开始在SQL Server数据库中查询XML。 I am having trouble with the most basic query. 我遇到了最基本的查询问题。 Here is a simplified example. 这是一个简化的例子。 How do I return description? 我如何返回描述? The SELECT statement below is what I am using, but it returns nothing. 下面的SELECT语句是我正在使用的,但它什么都不返回。

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

This is the snippet of the XML file that I am using: 这是我正在使用的XML文件的片段:

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

Well, you're missing out on the XML namespace! 好吧,你错过了XML命名空间! :-) :-)

Try this: 试试这个:

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

The magic is the 神奇的是

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

part here - it declares a namespace (with a prefix of your choice - can be anything - here 'x') for the period of the query on that XML data. 部分在这里 - 它在该XML数据的查询期间声明了一个名称空间(带有你选择的前缀 - 可以是任何东西 - 这里是'x')。

Hopefully, that'll return something! 希望,这会回归一些东西! ;-) ;-)

Marc

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

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