--1,我们先建一个表:Student(id,content /xml)

Create table Student
(
 StudentID int primary key ,
 StudentInfor xml
)

 

--2,插入数据
insert into dbo.Student values(100,'
<students>

<student >
<name>小李</name>
<age>22</age>
<birthday>1990-03-3</birthday>
</student>

<student >
<name>小王</name>
<age>21</age>
<birthday>1983-03-3</birthday>
</student>

</students>

')

 

<students>
  <student >
    <name>小王</name>
    <age>22</age>
    <birthday>1990-03-3</birthday>
  </student>
  <student >
    <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 >
<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[@])[1]
')

 

--4,添加字段add,用到modify的insert into语句。

update dbo.Student set StudentInfor.modify('
insert <phone>8333359</phone> as last into (/students/student[@])[1]

')

 

--5,删除学生节点,用到modify的delete语句,[@]为删除的条件,像t-sql中的where一样。
update dbo.Student set StudentInfor.modify('
delete (/students/student[@])

')

 

--6,更改学生节点字段,用到modify语句中的replace语句,text()表示的是add节点的值。
update dbo.Student set StudentInfor.modify('

replace value of(/students/student[@


')

 

--7,更改学生节点属性,用到modify语句中的replace语句,@id表示的是add节点的属性的值。

update dbo.Student set StudentInfor.modify('
replace value of(/students/student[@]/@id)[1]
with "114669"
')

 

相关文章:

  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-01-21
  • 2021-10-08
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2021-11-01
  • 2021-09-27
  • 2022-12-23
  • 2021-07-13
  • 2021-07-17
  • 2022-02-19
相关资源
相似解决方案