位置: 编程技术 - 正文

SQL Server中的XML数据进行insert、update、delete

编辑:rootadmin
SQL Server中新增加了XML.Modify()方法,分别为xml.modify(insert),xml.modify(delete),xml.modify(replace)对应XML的插入,删除和修改操作。 本文以下面XML为例,对三种DML进行说明: declare @XMLVar xml = ' <catalog> <book category="ITPro"> <title>Windows Step By Step</title> <author>Bill Zack</author> <price>.</price> </book> <book category="Developer"> <title>Developing ADO .NET</title> <author>Andrew Brust</author> <price>.</price> </book> <book category="ITPro"> <title>Windows Cluster Server</title> <author>Stephen Forte</author> <price>.</price> </book> </catalog> ' 1.XML.Modify(Insert)语句介绍 A.利用as first,at last,before,after四个参数将元素插入指定的位置 set @XMLVar.modify ( 'insert <first name="at first" /> as first into (/catalog[1]/book[1])' ) set @XMLVar.modify ( 'insert <last name="at last"/> as last into (/catalog[1]/book[1])' ) set @XMLVar.modify ( 'insert <before name="before"/> before (/catalog[1]/book[1]/author[1])' ) set @XMLVar.modify ( 'insert <after name="after"/> after (/catalog[1]/book[1]/author[1])' ) SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: <book category="ITPro" > <first name="at first" /> <title>Windows Step By Step</title> <before name="before" /> <author>Bill Zack</author> <after name="after" /> <price>.</price> <last name="at last" /> </book> B.将多个元素插入文档中 --方法一:利用变量进行插入 DECLARE @newFeatures xml; SET @newFeatures = N' <first>one element</first> <second>second element</second>' SET @XMLVar.modify(' ) insert sql:variable("@newFeatures") into (/catalog[1]/book[1])' --方法二:直接插入 set @XMLVar.modify(' ) insert (<first>one element</first>,<second>second element</second>) into (/catalog[1]/book[1]/author[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: < book category ="ITPro" > 2: < title > Windows Step By Step</ title > 3: < author > Bill Zack 4: < first > one element</ first > 5: < second > second element</ second > 6: </ author > 7: < price > .</ price > 8: < first > one element</ first > 9: < second > second element</ second > : </ book > C.将属性插入文档中 --使用变量插入 declare @var nvarchar() = '变量插入' set @XMLVar.modify( 'insert (attribute var {sql:variable("@var")}) ) into (/catalog[1]/book[1])' --直接插入 set @XMLVar.modify( 'insert (attribute name {"直接插入"}) ) into (/catalog[1]/book[1]/title[1])' --多值插入 set @XMLVar.modify( 'insert (attribute Id {"多值插入1"},attribute name {"多值插入2"}) ) into (/catalog[1]/book[1]/author[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: <book category="ITPro" var="变量插入" > 2: <title name="直接插入" >Windows Step By Step</title> 3: <author Id="多值插入1" name="多值插入2" >Bill Zack</author> 4: <price>.</price> 5: </book> D.插入文本节点 set @XMLVar.modify ( 'insert text{"at first"} as first ) into (/catalog[1]/book[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: < book category ="ITPro" > 2: at first 3: < title > Windows Step By Step</ title > 4: < author > Bill Zack</ author > 5: < price > .</ price > 6: </ book > 注意:插入本文同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法 E.插入注释节点 set @XMLVar.modify( 'insert <!--插入评论--> ) before (/catalog[1]/book[1]/title[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: <book category="ITPro" > 2: <!--插入评论--> 3: <title>Windows Step By Step</title> 4: <author>Bill Zack</author> 5: <price>.</price> 6: </book> 注意插入注释节点同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法 F.插入处理指令 set @XMLVar.modify( 'insert <?Program "Instructions.exe" ?> ) before (/catalog[1]/book[1]/title[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: <bookcategory="ITPro"> 2: <?Program"Instructions.exe"?> 3: <title>Windows Step By Step</title> 4: <author>Bill Zack</author> 5: <price>.</price> 6: </book> 注意插入处理指令同样具体 as first,as last,before,after四种选项,可以参考A中的使用方法 G.根据 if 条件语句进行插入 set @XMLVar.modify( 'insert ) if (/catalog[1]/book[1]/title[2]) then text{"this is a 1 step"} else ( text{"this is a 2 step"} ) into (/catalog[1]/book[1]/price[1])' SELECT @XMLVar.query('/catalog[1]/book[1]' ); 结果集为: 1: <book category="ITPro"> 2: <title>Windows Step By Step</title> 3: <author>Bill Zack</author> 4: <price>.this isa 2 step</price> 5: </book> 2.XML.Modify(delete)语句介绍 --删除属性 set @XMLVar.modify('delete /catalog[1]/book[1]/@category') --删除节点 set @XMLVar.modify('delete /catalog[1]/book[1]/title[1]') --删除内容 set @XMLVar.modify('delete /catalog[1]/book[1]/author[1]/text()') --全部删除 set @XMLVar.modify('delete /catalog[1]/book[2]') SELECT @XMLVar.query('/catalog[1]'); 结果集为: 1: <catalog> 2: <book> 3: <author /> 4: <price>.</price> 5: </book> 6: <book category="ITPro"> 7: <title>Windows Cluster Server</title> 8: <author>Stephen Forte</author> 9: <price>.</price> : </book> : </catalog> 3.XML.Modify(replace)语句介绍 --替换属性 set @XMLVar.modify('replace value of(/catalog[1]/book[1]/@category)) with ("替换属性")' --替换内容 set @XMLVar.modify('replace value of(/catalog[1]/book[1]/author[1]/text()[1])) with("替换内容")' --条件替换 set @XMLVar.modify('replace value of (/catalog[1]/book[2]/@category)) with( if(count(/catalog[1]/book)>4) then "条件替换1" else "条件替换2")' SELECT @XMLVar.query('/catalog[1]' ); 结果集为: 1: <catalog> 2: <bookcategory="替换属性"> 3: <title>Windows Step By Step</title> 4: <author>替换内容</author> 5: <price>.</price> 6: </book> 7: <bookcategory="条件替换2"> 8: <title> Developing ADO .NET</title> 9: <author> Andrew Brust</author> : <price>.</price> : </book> : <bookcategory="ITPro"> : <title>Windows Cluster Server</title> : <author>Stephen Forte</author> : <price>.</price> : </book> : </catalog>

推荐整理分享SQL Server中的XML数据进行insert、update、delete,希望有所帮助,仅作参考,欢迎阅读内容。

SQL Server中的XML数据进行insert、update、delete

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

在安装sql中或安装后sa用户无法登陆系统解决方法 具体的操作步骤如下:1.打开SQLServerManagementStudio,连接时,身份验证使用"使用windows身份验证"2.右击连接实例的属性,选择安全性,将服务器身份验证选择S

SQL 自动备份的脚本 此版本是始终只有最新的一版(我自己的用的是版本是每天都有一个备份)declare@DBNamevarchar()set@DBName='ReportServer$SQL'--截断日志DUMPTRANSACTION@DBNameWITHNO

在SQLServer 中编写存储过程 然而,在SQLServer中,我们可以用.NET家族的语言——主要是VB.NET和C#来编写存储过程(以及方法、触发器和其它组件)。让我们来熟悉一下关于编写存

标签: SQL Server中的XML数据进行insert、update、delete

本文链接地址:https://www.jiuchutong.com/biancheng/319263.html 转载请保留说明!

上一篇:SQL2005 ROW_NUMER实现分页的两种常用方式(sql中的row_number)

下一篇:在安装sql2005中或安装后sa用户无法登陆系统解决方法(sql2005安装方法)

  • 稳岗返还入什么科目
  • 社保次月什么时候到账
  • 工会经费的减免申请
  • 企业增资认缴是什么意思
  • 企业最常用的业务分录
  • 总分机构移送固定资产是否缴增值税
  • 企业所得税研发费用100%扣除的有哪些企业
  • 房地产土地增值税的算法
  • 没有票据怎么要账
  • 研发试制的样机属于固定资产吗
  • 企业工资薪酬包括哪些内容
  • 不需要抵扣的怎么直接进费用
  • 怎样算纳税人
  • 网上认证服务费可以全额抵扣吗
  • 契税如果没有按时交会怎么办
  • 专用发票退回
  • 股东借款作为资本金投入
  • ubuntu18.04中文
  • mac软件打开提示意外退出
  • 在windows7中,使用鼠标拖放功能
  • 财产租赁所得个人所得税怎么申报
  • 广告公司文化事业费怎么交
  • Win11如何卸载microsoft edge浏览器
  • linux版笔记本什么意思
  • 资产负债表中的固定资产怎么算
  • php基础语法
  • 以前年度多计提的附加税怎么冲回
  • edi文件怎么打开
  • 现金流方法
  • php use function
  • php7.3安装
  • 报税货物范围包括
  • php字符串定义
  • 大前端入门指南
  • 基于深度学习的图像超分辨率——综述
  • utermux命令
  • 收到进项发票不对公账怎么办
  • 小型微利企业如何进行税收筹划
  • 生产劳务成本会计分录
  • 滴滴出行发票税率是多少
  • 企业注销后还有风险吗
  • 小微企业的所得税税率是多少
  • MySQL Community Server压缩包安装配置方法
  • mysql中事件的作用
  • sqlite3 top的查询及limit语法介绍
  • 报税中的利润表怎么填
  • 长期借调人员工资福利
  • 实收资本变化
  • 个人去税务局开劳务费怎么开
  • 现金流量表的编制公式 计算公式
  • 什么叫查账征收和核定征收
  • 费用发票已报销怎么入账
  • 公司与公司之间合作协议
  • 销售预付卡的账务处理
  • 非税收入定额票据是什么
  • 外商投资企业开什么发票
  • sqlserver数据库恢复挂起状态
  • mysql常用命令
  • sql %和_
  • win8创建密码
  • microsoft window vista
  • 微软公布新企业AI产品定价 股价应声涨至盘中纪录高位
  • 怎么修改ubuntu的ip地址
  • xp3软件
  • window10通知
  • application error message 漏洞验证
  • win10安装字体的操作步骤
  • oem预装系统
  • linux配置文件命令
  • 如何使用灭火器
  • android游戏开发框架
  • cocos设置锚点
  • 网页设计div css布局
  • jquery层级选择器
  • 世界坐标转换成屏幕坐标
  • node.js gui
  • python放歌
  • jquery实现搜索功能
  • 怎样查询退休审核表
  • 宾馆增值税专用发票可以抵扣
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设