sql server 中xml 数据类型的insert、update、delete
近日对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
sql server 中xml 数据类型的insert、update、delete的更多相关文章
- 基于SQL Server日志链查看数据库insert/update/delete操作(一)
在MSSQLServer2008下的语句 不同版本可能语句会有微小差别 SELECT [Slot ID], [Transaction ID], Operation, AllocUnitName, [C ...
- SQL SERVER中XML查询:FOR XML指定PATH
SQL SERVER中XML查询:FOR XML指定PATH 前言 在SQL SERVER中,XML查询能够指定RAW,AUTO,EXPLICIT,PATH.本文用一些实例介绍SQL SERVER中指 ...
- SQL Server 中使用数据类型表示小数
在使用的时候发现一个问题,由于编程的习惯,当数据库中需要存储小数的时候,就想当然的使用了float类型,可结果太让人意外了. 数据库中存储了0.5没问题,当使用0.6的时候,得到的确是0.599999 ...
- SQL Server中的数据类型
参考 SQL Server 2012编程入门经典(第4版) SQL Server 自带的数据类型 整型: 货币 近似小数 日期/时间 特殊数字 字符 Unicode 二进制 其他
- SQL Server 对XML数据类型的SQL语句总结
--创建XMLTable , ) primary key, XMLCol xml); go ------------------------------------------------------ ...
- sql server中同时执行select和update语句死锁问题
原始出处 http://oecpby.blog.51cto.com/2203338/457054 最近在项目中使用SqlServer的时候发现在高并发情况下,频繁更新和频繁查询引发死锁.通常我们知道如 ...
- SQL Server 中SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...
- SQL系列(十二)—— insert update delete
前言 这个系列的前面都一直在介绍查询select.但是SQL中十分广泛,按对数据的不同处理可以分为: DML:全称Data Manipulation Language,从名字上可以看出,DML是对数据 ...
- sql server中对xml进行操作
一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XM ...
随机推荐
- PHP基础教程-54课-问题
question: $arr = array(1,2,3,4); /*如何通过foreach 将数组变成 $arr = arry(2,4,6,8) */ 起初用: $arr = array(1,2,3 ...
- Bootstrap <基础八>图片
Bootstrap 提供了三个可对图片应用简单样式的 class: .img-rounded:添加 border-radius:6px 来获得图片圆角. .img-circle:添加 border-r ...
- Redis集群部署
1.1.1redis简介 Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志 型. Key-Value数据库 1.1.2redis常见使用场景 1.会话缓存(S ...
- 在CDH5.5.0上安装Kudu6.0
1. 下载安装文件: a. CSD文件:KUDU-0.6.0.jar b. kudu parcel:KUDU-0.6.0-1.kudu0.6.0.p0.334-el6.parcel和manifest. ...
- java读取大文件
1 多线程 2 java内存映射读取大文件
- ajax里面success函数return上层接收不到
开发一个小功能,在success fail里直接return,没有收到返回值.排查,查了下往上的博客,参考了以下三个: http://blog.csdn.net/fairyhawk/article/d ...
- IOS App 右上脚红色数字提醒
IOS8.0以前直接显示: UIApplication *application=[UIApplication sharedApplication]; //设置图标上的更新数字 application ...
- JSTL标签库的使用
首先是四大标签库 核心 标签库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"%> 格式标签库 <%@ ta ...
- html canvas 弹球(模仿)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 实验二 简易版C语言文法
<程序>::=begin<语句串>end <语句串>::=<语句>{;<语句>} <语句>::=<赋值语句> < ...