从SQL Server中的XML列获取数据 [英] Get data from XML column in SQL server

查看:297
本文介绍了从SQL Server中的XML列获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server数据库表中有一个XML列。



i想从XML列中获取结果。



我该怎么做以下是我的表格栏



I have a XML column in SQL Server Database table.

i Want to get Result from that XML Column.

How can i do this Below is my Table Column

<string xmlns="http://www.webserviceX.NET">
  <NewDataSet>
    <Table>
      <Country>Sri Lanka</Country>
      <City>Katunayake</City>
    </Table>
    <Table>
      <Country>Sri Lanka</Country>
      <City>Anuradhapura</City>
    </Table>
    <Table>
      <Country>Sri Lanka</Country>
      <City>Batticaloa</City>
    </Table>
    <Table>
      <Country>Sri Lanka</Country>
      <City>Ratmalana</City>
    </Table>
    <Table>
      <Country>Sri Lanka</Country>
      <City>Trincomalee</City>
    </Table>
  </NewDataSet>
</string>





我尝试过:





What I have tried:

SELECT
    [Xml_Data].value('(string/NewDataSet/Table/Country)[1]', 'nvarchar(max)') as FirstName
    ,[Xml_Data].value('(string/NewDataSet/Table/City)[1]', 'nvarchar(max)') as LastName
FROM [AP_DEMO]

推荐答案

这是因为 XMLNS

尝试以下查询,应该完美的工作。

It's because of the XMLNS.
Try following query and should work perfectly.
;WITH XMLNAMESPACES (N'http://www.webserviceX.NET' as X)
SELECT
    [Xml_Data].value('(/X:string/X:NewDataSet/X:Table/X:Country)[1]', 'nvarchar(max)') as FirstName
    ,[Xml_Data].value('(/X:string/X:NewDataSet/X:Table/X:City)[1]', 'nvarchar(max)') as LastName
FROM [AP_DEMO]





如果您只是删除xmlns命名空间,旧的查询仍然有效。



进一步阅读:将命名空间添加到查询中使用XMLNAMESPACES | Microsoft Docs [ ^ ]



希望,有帮助:)



Your old query will still work if you just remove the xmlns namespace.

Further reading : Add Namespaces to Queries with WITH XMLNAMESPACES | Microsoft Docs[^]

Hope, it helps :)


这篇关于从SQL Server中的XML列获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆