本篇内容介绍了“如何掌握SQL Server的XML类型,有哪些要点”的有关知识,在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、创建测试数据,指定字段数据类型为XML
1、创建表
--创建表,包含Xml类型列
CREATE TABLE Person
(
Id int,
Info xml
)
2、插入测试数据
--插入3条测试数据
INSERT Person VALUES(1,'<Person><ID>1</ID><Name>刘备</Name></Person>')
INSERT Person VALUES(2,'<Person><ID>2</ID><Name>关羽</Name></Person>')
INSERT Person VALUES(3,'<Person><ID>3</ID><Name>张飞</Name></Person>')
3、插入XML文件数据
insert Person values(4,select * from openrowset(bulk 'G:\Document\XMLDocument\x3.xml',single_clob) as x)
4、创建索引
--XML“主”索引
create primary xml index IX_Person_Info
on Person ( Info );
--XML“路径”辅助索引
create xml index IX_Person_Info_Path
on Person ( Info )
using xml index IX_Person_Info for path;
--XML“属性”辅助索引
create xml index IX_Person_Info_Property
on Person ( Info )
using xml index IX_Person_Info for property;
--XML“内容”辅助索引
create xml index IX_Person_Info_value
on Person ( Info )
using xml index IX_Person_Info for value;
二、查询XML数据
T-SQL 支持用于查询 XML 数据类型的 XQuery 语言。
XQuery 基于现有的 XPath 查询语言,并支持更好的迭代、更好的排序结果以及构造必需的 XML 的功能。
1、query(XPath条件):返回xml 类型的节点内容
--查询节点内容query()方法
SELECT Id,Info.query('(/Person/Name)[1]') FROM Person WHERE ID = 2
复杂查询
declare @myxml xml
set @myxml='<people>
<student id="201301">
<Name>王五</Name>
<Age>18</Age>
<Address>湖南</Address>
</student>
<student id="201302">
<Name>李一</Name>
<Age>20</Age>
<Address>湖北</Address>
</student>
</people>'
select @myxml.query('
for $ss in /people/student
where $ss/Age[text()]<22
return element Res
{
(attribute age{data($ss/Age[text()[1]])})
}')
结果为: <Res age="18" /><Res age="20" />
一个完整实例:
declare @x xml;
set @x = '
<root>
<people id="001">
<student id="1">
<name>彪</name>
<name>阿彪</name>
<type>流氓</type>
</student >
</people>
<people id="002">
<student id="2">
<name>光辉</name>
<name>二辉</name>
<type>流氓</type>
</student >
</people>
<people id="001">
<student id="3">
<name>小德</name>
<name>小D</name>
<type>臭流氓</type>
</student >
</people>
</root>';
--1、取root的所有子节点
select @x.query('root'), @x.query('/root'), @x.query('.');
--/*注释:
-- 这里实际上是取所有节点,root 必须是最高级节点名称,当换成任意子节点都是取不到值的
--*/
--2、取 student 的所有子节点,不管 student 在文档中的位置。
select @x.query('//student ');
--3、取people下 所有 name
select @x.query('//people//name');
--4、取属性为id 的所有节点
select @x.query('//student [@id]');
/*注释:
XQuery不支持直接顶级 attribute 节点,必须附带上对节点的查找
属性必须要加[]
*/
--5、选取属于 root 子元素的第一个 people 元素。
select @x.query('/root/people[1]');
--6、选取属于 root 子元素的最后一个 people 元素。
select @x.query('/root/people[last()]');
--7、选取属于 root 子元素的倒数第二个 people 元素。
select @x.query('/root/people[last()-1]');
--8、选取最前面的两个属于 root 元素的子元素的 people 元素。
select @x.query('/root/people[position()<3]');
--9、选取 root 元素的所有 student 元素,且其中的属性 id 的值须大于 1。
select @x.query('/root//student [@id>1]');
----10、 root 元素的所有 student 元素,且其中的属性 id 的值须大于 1 并且子节点 name 的值为 光辉 的。
select @x.query('/root/people[./student [@id>1 and name="光辉"]]');
--11、选取 root 子元素的所有 people 元素,且 属性id 的值须大于 为001 子元素student 属性 id 的值为 1的
select @x.query('/root/people[@id="001" and ./student [@id=1]]');
“如何掌握SQL Server的XML类型,有哪些要点”的内容就介绍到这里了,感谢大家的阅读。