2009-08-19 134 views
4

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

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

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

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

回答

11

好了,你在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" 

一部分 - 它声明了一个命名空间(与您选择的前缀 - 可以是任何东西 - 这里的“x ')的XML数据查询期间。

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

Marc