--1,我们先建一个表:Student(id,content /xml)
Create table Student
(
StudentID int primary key ,
StudentInfor xml
)
--2,插入数据
insert into dbo.Student values(100,'
<students>
<student id="100">
<name>小李</name>
<age>22</age>
<birthday>1990-03-3</birthday>
</student>
<student id="101">
<name>小王</name>
<age>21</age>
<birthday>1983-03-3</birthday>
</student>
</students>
')
<students>
<student id="100">
<name>小王</name>
<age>22</age>
<birthday>1990-03-3</birthday>
</student>
<student id="114669">
<name>这是一个神奇的网站!</name>
<age>21</age>
<birthday>1983-03-3</birthday>
</student>
</students>
-- 2,添加学生节点,就是添加一个学生,用到modify的insert into语句,后面的/为xml节点的路径。
update dbo.Student set StudentInfor.modify('
insert
<student id="102">
<name>小张</name>
<age>44</age>
<birthday>1991-05-25</birthday>
</student>
as last into(/students)[1]
')
--3,添加属性,用到modify的insert into语句。
update dbo.Student set StudentInfor.modify('
insert attribute class {"男"} into (/students/student[@id="102"])[1]
')
--4,添加字段add,用到modify的insert into语句。
update dbo.Student set StudentInfor.modify('
insert <phone>8333359</phone> as last into (/students/student[@id="102"])[1]
')
--5,删除学生节点,用到modify的delete语句,[@id="1003"]为删除的条件,像t-sql中的where一样。
update dbo.Student set StudentInfor.modify('
delete (/students/student[@id="102"])
')
--6,更改学生节点字段,用到modify语句中的replace语句,text()表示的是add节点的值。
update dbo.Student set StudentInfor.modify('
replace value of(/students/student[@id="101"]/name/text())[1] with "这是一个神奇的网站!"
')
--7,更改学生节点属性,用到modify语句中的replace语句,@id表示的是add节点的属性的值。
update dbo.Student set StudentInfor.modify('
replace value of(/students/student[@id="101"]/@id)[1]
with "114669"
')