T-SQL 查询XML
我们经常在SQL Server列中存一些XML来作为配置文件或者是保存特殊信息,那么如何将其展开并查询它或将其呈现为关系数据? 其实在T-SQL 下可以很容易的实现。
示例xml
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>
我们先把xml插入到一个临时表中,只有两个字段ConfigName和ConfigXML
create table #config
(
ConfigName varchar(100),
ConfigXML xml
) insert into #config
select 'TestConfig', '<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer''s Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>'
好的,下面来看一下场景是如何用sql 实现的。
- 获取所有xml 中各个book的子xml内容, 这种情况出现在为前台系统提供xml片段。
查询语句为
select r.value('@id','varchar(50)') as bookid,
config.r.query('.') as xmlconfig
from #config
cross apply ConfigXML.nodes('/catalog/book') as config(r)
where ConfigName = 'TestConfig'
- 将各个节点转换为关系数据库表结构
查询语句为
select config.r.value('@id', 'varchar(50)') AS BookID,
config.r.value('(author/text())[1]', 'varchar(50)') AS author,
config.r.value('(title/text())[1]', 'varchar(50)') AS title,
config.r.value('(genre/text())[1]', 'varchar(50)') AS genre,
config.r.value('(price/text())[1]', 'varchar(50)') AS price,
config.r.value('(publish_date/text())[1]', 'varchar(50)') AS publish_date
from #config
cross apply ConfigXML.nodes('/catalog/book') as config(r)
where ConfigName = 'TestConfig'
查询 bookid为k101的信息
方式1: 直接在2的结果集中加上一个where条件and config.r.value('@id','varchar(50)') = 'bk101'
select config.r.value('@id', 'varchar(50)') AS BookID,
config.r.value('(author/text())[1]', 'varchar(50)') AS author,
config.r.value('(title/text())[1]', 'varchar(50)') AS title,
config.r.value('(genre/text())[1]', 'varchar(50)') AS genre,
config.r.value('(price/text())[1]', 'varchar(50)') AS price,
config.r.value('(publish_date/text())[1]', 'varchar(50)') AS publish_date
from #config
cross apply ConfigXML.nodes('/catalog/book') as config(r)
where ConfigName = 'TestConfig'
and config.r.value('@id','varchar(50)') = 'bk101'
方式2:修改cross apply的xml path
select config.r.value('@id', 'varchar(50)') AS BookID,
config.r.value('(author/text())[1]', 'varchar(50)') AS author,
config.r.value('(title/text())[1]', 'varchar(50)') AS title,
config.r.value('(genre/text())[1]', 'varchar(50)') AS genre,
config.r.value('(price/text())[1]', 'varchar(50)') AS price,
config.r.value('(publish_date/text())[1]', 'varchar(50)') AS publish_date
from #config
cross apply ConfigXML.nodes('/catalog/book[@id=''bk101'']') as config(r)
where ConfigName = 'TestConfig'
注:如果我们从sql server参数里面拼接xpath需要添加sql:variable来表示他是一个sql server变量而不是xml属性名。
declare @bookid varchar(30) = 'bk101'
select config.r.value('@id', 'varchar(50)') AS BookID,
config.r.value('(author/text())[1]', 'varchar(50)') AS author,
config.r.value('(title/text())[1]', 'varchar(50)') AS title,
config.r.value('(genre/text())[1]', 'varchar(50)') AS genre,
config.r.value('(price/text())[1]', 'varchar(50)') AS price,
config.r.value('(publish_date/text())[1]', 'varchar(50)') AS publish_date
from #config
cross apply ConfigXML.nodes('/catalog/book[@id=sql:variable("@bookid")]') as config(r)
where ConfigName = 'TestConfig'
4. 查询每个book 的id和author信息
方式1: 使用第二步结果集
select config.r.value('@id', 'varchar(50)') AS BookID,
config.r.value('(author/text())[1]', 'varchar(50)') AS author
from #config
cross apply ConfigXML.nodes('/catalog/book') as config(r)
where ConfigName = 'TestConfig'
方式2:
select config.r.value('../@id', 'varchar(50)') AS BookID,
config.r.value('.', 'varchar(50)') AS author
from #config
cross apply ConfigXML.nodes('/catalog/book/author') as config(r)
where ConfigName = 'TestConfig'
T-SQL 查询XML的更多相关文章
- sql查询XML
--查询Extra里节点UName值等于“黄”的所有信息 select * from t_UserPayLog where Extra.exist('//UName[.="黄"]' ...
- SQL Server XML 查询
[参考1] 18个小实例入门SQLServer XML查询 [参考2] 转载---SQL Server XML基础学习之<5>--XQuery(query)
- Sql Server xml 类型字段的增删改查
1.定义表结构 在MSSM中新建数据库表CommunicateItem,定义其中一个字段ItemContentXml 为xml类型 2.编辑表数据,新增一行,发现xml类型不能通过设计器录入数据. 需 ...
- Hibernated的sql查询
记录一下学习Hibernate的心得 1.为什么HIbernate会支持原生态的sql查询? HQL查询语句虽然方便我们查询,但是基于HQL的查询会将查询出来的对象保存到hibernate的缓存当中, ...
- 15个初学者必看的基础SQL查询语句
本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 本文将分享15个初学者必看的基础SQL查询语句,都很基础,但是你不一定都会,所以好好看看吧. 1.创建表和数据插 ...
- hibernate将本地SQL查询结果封装成对象
hibernate将本地SQL查询结果封装成对象 不知道大家有没有碰过这种情况,迫于很多情况只能用native SQL来查询(如:复杂统计等),然而使用native查询后,结果会被放到object里, ...
- SQL查询性能分析
http://blog.csdn.net/dba_huangzj/article/details/8300784 SQL查询性能的好坏直接影响到整个数据库的价值,对此,必须郑重对待. SQL Serv ...
- SQL SERVER XML 学习总结
SQL SERVER XML 学习总结 最新的项目任务要做一个数据同步的功能,这些天都在做技术准备,主要是用到了微软的Service Broker技术,在熟悉使用该技术的同时,又用到了Sql s ...
- 转载---SQL Server XML基础学习之<6>--XQuery的 value() 方法、 exist() 方法 和 nodes() 方法
/*------------------------------------------------------------------------------+ #| = : = : = : = : ...
- 转载---SQL Server XML基础学习之<5>--XQuery(query)
本章写一些SQL Server XML的一些XQuery基础语法,主要讲的query查询语法 T-SQL 支持用于查询 XML 数据类型的 XQuery 语言的子集. XQuery 基于现有的 XPa ...
随机推荐
- 对于前端,「微信小程序」其实不美好
微信小程序开放公测了,9月底我曾经写过一篇 「微信小程序」来了,其中最后一句:"谢天谢地,我居然还是个前端". 这种火爆的新事物总是令人激动,感谢这个时代. 但是,当我真作为开发者 ...
- ABP教程-通过ABPboilerplate模版创建项目
开篇说明: 此篇博客是属于半教程博客,为什么说是半教程呢.因为我不会打算说什么理论性的东西,没必要.要看理论性的资料以及基础信息,请前往tkb至简和@阳光铭睿的博客查看文档资料. 开发环境: vs20 ...
- angularjs集成requirejs
其实说成使用requirejs加载angularjs应用会更贴切一些 <body> <span ng-controller="homeController"> ...
- 阿里云系列——6.给你的域名使用CDN加速(详细步骤+简单配置)
网站部署之~阿里云系列汇总 http://www.cnblogs.com/dunitian/p/4958462.html 进入管理页面:https://home.console.aliyun.com/ ...
- angularJS实用的开发技巧
一.开端 真的是忙完这一阵子就可以忙下一阵子了啊... 最近在做一个angularJS+Ionic的移动端项目...记录一些技巧,方便自己以后查阅,也方便需要的人可以看一看...^_^ 二.基础原则了 ...
- Java 哈希表运用-LeetCode 1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- SQL Tuning 基础概述08 - SQL Tuning Advisor
SQL调优顾问 SQL Tuning Advisor的使用案例: 1.构建测试表T 2.定义调整任务 3.修改调整任务参数 4.执行调整任务 5.监控调整任务 6.查看调整任务建议 7.删除调整任务 ...
- 关于css清除浮动,解决内容溢出的问题
以前在布局的时候总会遇到这样的问题,比如我想让整体的内容居中,所以会这样写, .main-content{ width:960px:height:300px;margin:0px auto; } 然后 ...
- javaWeb应用打包
在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- Rafy 框架 - 为数据库生成注释
当开发者使用 CodeFirst 开发模式,编写了大量的实体类,在代码中编写了完整的类型注释和属性注释,并自动生成数据库后,往往需要把实体类型和实体属性上的注释同时生成到对应的数据库表及字段上.这样, ...