SQL Server 中的 XML 查询 [英] XML Query within SQL Server
问题描述
我刚刚开始在 SQL Server 数据库中查询 XML.我在最基本的查询上遇到了问题.这是一个简化的示例.我如何返回描述?下面的 SELECT 语句是我正在使用的,但它什么都不返回.
I just starting to query XML within a SQL Server database. 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 Incidents.IncidentXML.query
('data(/dsIncident/IncidentInformation/Description)') AS Description
FROM Incidents
这是我正在使用的 XML 文件的片段:
This is the snippet of the XML file that I am using:
<dsIncident xmlns="http://tempuri.org/dsIncident.xsd">
<IncidentInformation>
<Description>This is the description.</Description>
<Country>Singapore</Country>
</IncidentInformation>
</dsIncident>
推荐答案
好吧,您错过了 XML 命名空间!:-)
Well, you're missing out on the XML namespace! :-)
试试这个:
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 数据的时间段.
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.
希望这会有所回报!;-)
Hopefully, that'll return something! ;-)
马克
这篇关于SQL Server 中的 XML 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!