SQL Server中的XML查询

Sch*_*tzE 5 xml sql sql-server xquery

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

SELECT Incidents.IncidentXML.query
('data(/dsIncident/IncidentInformation/Description)') AS Description 
FROM Incidents
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的XML文件的片段:

<dsIncident xmlns="http://tempuri.org/dsIncident.xsd">
  <IncidentInformation>
    <Description>This is the description.</Description>
    <Country>Singapore</Country>
  </IncidentInformation>
</dsIncident>
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 12

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

试试这个:

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

神奇的是

declare namespace x="http://tempuri.org/dsIncident.xsd"
Run Code Online (Sandbox Code Playgroud)

部分在这里 - 它在该XML数据的查询期间声明了一个名称空间(带有你选择的前缀 - 可以是任何东西 - 这里是'x').

希望,这会回归一些东西!;-)