博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql server 中xml 数据类型的insert、update、delete
阅读量:5158 次
发布时间:2019-06-13

本文共 4678 字,大约阅读时间需要 15 分钟。

近日对SQL操作XML作了如下整理:

1、插入 XML

DECLARE @myDoc XML

SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features> </Features>
</ProductDescription>
</Root>'
/*将元素节点插入到文档中*/
--在Features里插入一个节点
SET @myDoc.modify(
'insert <Populate>Put your things into basket of bike</Populate>
into (/Root/ProductDescription/Features)[1]');
SELECT @myDoc;
--当前插入的节点为Features中第一个节点
SET @myDoc.modify('
insert <ride>people could ride bike</ride>
as first into (/Root/ProductDescription/Features)[1]');
SELECT @myDoc;
--当前插入的节点为Features中最后一个节点
SET @myDoc.modify('
insert <function> people use it as transport</function>
as last into (/Root/ProductDescription/Features)[1]');
SELECT @myDoc;
--当前插入的节点放在<ride>标签的后面
SET @myDoc.modify('
insert <sport>ride bike is a sport</sport>
after(/Root/ProductDescription/Features/ride)[1]');
SELECT @myDoc;
--------------------------------------
/*将多个元素插入到文档中*/
DECLARE @myDoc2 XML
SET @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features> </Features>
</ProductDescription>
</Root>'
DECLARE @NewFeatures XML
SET @NewFeatures = N'<ride>people could ride bike</ride>
<sport>ride bike is a sport</sport>'

SET @myDoc.modify('

insert sql:variable("@NewFeatures")
into (/Root/ProductDescription/Features)[1]')
SELECT @myDoc;
------------------------------------
--插入属性到文档中
DECLARE @myDoc xml;
SET @myDoc =
'<Root>
<Location LocationID="10" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';
--在Location节点中插入一个number属性,其值为5
SET @myDoc.modify('
insert attribute number {"5"}
into (/Root/Location[@LocationID=10])[1]')
SELECT @myDoc;
--在Location节点中插入一个变量
DECLARE @hour INT
SET @hour = 2;
SET @myDoc.modify('
insert attribute hour {sql:variable("@hour")}
into (/Root/Location[@LocationID=10])[1]')
SELECT @myDoc;
--------------------------------------------
--向表中类型为XML字段,增加新的节点
IF OBJECT_ID('T') IS NOT NULL DROP TABLE T
CREATE TABLE T (i int, x xml);
go
INSERT INTO T VALUES(1,'<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>');
go
UPDATE T
SET x.modify('
insert <ride>people could ride bike</ride>
after (/Root/ProductDescription/Features/Maintenance)[1]')
SELECT x.query('/Root/ProductDescription/Features') FROM T
-----------------------------------
--根据if 条件进行插入
DECLARE @myDoc xml;
SET @myDoc =
'<Root>
<Location LocationID="10" LaborHours="1.2" >
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>';
--满足当前条件添加一个新的hour属性
SET @myDoc.modify('
insert
if (/Root/Location[@LocationID=10])
then attribute hour {"5"}
else ()
into (/Root/Location[@LocationID=10])[1]')
SELECT @myDoc;
--满足当前条件添加一个新的节点
SET @myDoc.modify('
insert
if (count(/Root/Location/step) <= 2)
then element step {"this is new step"}
else ()
as first into (/Root/Location)[1]')
SELECT @myDoc;

2、更新XML

DECLARE @myDoc xml

SET @myDoc = '<Root>
<Location LocationID="10"
LaborHours="1.1"
MachineHours=".2" >Manufacturing steps are described here.
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
SELECT @myDoc;
--替换节点Location中LaborHours属性的值为100
SET @myDoc.modify('
replace value of (/Root/Location/@LaborHours)[1]
with "100"')
SELECT @myDoc;
--使用表更新另一个表中类型为XML的字段的属性
DECLARE @Friend TABLE
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
Friend XML
)
INSERT INTO @Friend SELECT '<Friends>
<friend name="junwenli" sex="man" age="23"></friend>
<friend name="jinhanliu" sex="man" age="24"></friend>
<friend name="fangcheng" sex="man" age="23"></friend>
</Friends>'

DECLARE @Temp TABLE

(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
FriendName NVARCHAR(32)
)
INSERT INTO @Temp SELECT 'GuoHu';

UPDATE F

SET Friend.modify('replace value of (Friends/friend/@name)[1] with sql:column("T.FriendName")')
FROM @Friend F,@Temp T
WHERE F.ID = T.ID;
SELECT Friend FROM @Friend;

3、删除XML

DECLARE @myDoc xml

SET @myDoc = '<?Instructions for=TheWC.exe ?>
<Root>
<!-- instructions for the 1st work center -->
<Location LocationID="10"
LaborHours="1.1"
MachineHours=".2" >Some text 1
<step>Manufacturing step 1 at this work center</step>
<step>Manufacturing step 2 at this work center</step>
</Location>
</Root>'
--删除属性MachineHours
SET @myDoc.modify('
delete /Root/Location/@MachineHours
')
SELECT @myDoc
--删除第二个step节点
SET @myDoc.modify('
delete /Root/Location/step[2]
')
SELECT @myDoc

转载于:https://www.cnblogs.com/windofwolf/p/4243771.html

你可能感兴趣的文章
【Ruby】Ruby在Windows上的安装
查看>>
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
[转载]mysql的left,right,substr,instr截取字符串,截取
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
摘抄详细的VUE生命周期
查看>>
javascript高级程序设计---js事件思维导图
查看>>
sprint计划会议
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>
How to properly set clock speed for STM32F4xx devices
查看>>
.net 分布式架构之分布式锁实现(转)
查看>>
吴恩达机器学习笔记 —— 3 线性回归回顾
查看>>
C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 用户密码安全增强
查看>>
1024 (程序员节) 欢乐赛 T1 分火腿 (思路题)
查看>>
在SpringMVC中自定义上下文的一些想法
查看>>
在libGDX中使用Spine骨骼动画
查看>>
Windows防火墙开启ping,禁ping的配置方法
查看>>