位置: 编程技术 - 正文

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安装方法)

  • 纳税义务发生时间记忆口诀
  • 印花税的纳税人是买方还是卖方
  • 新成立小规模纳税人开发票如何办理
  • 8.会计核算方法具体包括哪些内容?
  • 社保计入主营业务成本
  • 小规模纳税人缴纳上月应交增值税
  • 反结账只能反结上月的账吗
  • 快递费专票可以报销吗
  • 资产负债表里的存货包括哪些科目
  • 财务报表的资产负债表怎么填
  • 一般借款资本化加权平均数
  • 上期留抵税额如何计算
  • 京东预付定金是什么意思
  • 溢价发行的债券,当债券临近到期日时,债券价格
  • 无形资产摊销起止时间
  • 半成品原材料报废会计处理怎么做?
  • 餐饮业购买餐桌椅会计分录
  • 民办幼儿园所得税优惠政策
  • 租赁房产税如何征收的2019
  • 收据能不能做账
  • 中小五金企业如何进行成本核算
  • 购买东西收据
  • 给子公司员工发放奖金合法吗
  • 房地产企业申请破产,原来的购房合同还有效吗?
  • 工会经费的会计分录2022
  • window10怎么用wifi上网
  • 回收站右键菜单修改
  • 金融资产有哪三类代码
  • 在win7中,打开资源管理器的快捷键
  • 如何解决win7系统蓝牙接收模块影响电脑蓝屏
  • 股票公允价值减值怎么做账
  • 谷歌浏览器历史记录插件
  • 扣除年度未扣除怎么计算
  • 税务 终结执行
  • 税法规定个人收入超过3500元的超过部分应
  • 处置子公司的方式
  • AttributeError: ‘bytes‘ object has no attribute ‘encode‘异常解决方案
  • 出口货物应退增值税如何计算
  • acc字幕文件怎么转换srt字幕
  • 确认应付职工薪酬如何计算
  • 其他权益工具确认投资损失的账务处理
  • 什么是预收账款减少
  • 帝国cms标签调用大全
  • python中事件处理的方法
  • 个体户怎么网上变更法人
  • 劳务派遣人员保险
  • 关于sqlserver数据库服务器登录账户的说法错误的是
  • db2导出数据到excel
  • 企业内部交易如何操作
  • 固定资产减少处理
  • 城镇土地使用税税率
  • 税控系统技术维护费全额抵扣分录
  • 劳动保护费是否可以抵扣
  • 核定征收所得税税率是多少
  • 收到银行本票计入什么科目
  • 委托加工农产品的扣除率
  • 母公司代发子公司工资,子公司申报个税
  • 外派国外的员工享受国内法定假期吗
  • 机票退票手续费多少
  • 企业代扣个人社保最新会计处理
  • 新发票和旧发票的区别
  • 企业建账前应考虑什么
  • 电脑开机密码忘记了怎么打开
  • win8.1怎么重新装系统
  • win10周年更新版是什么意思
  • windows7访问windows10
  • win81和win10
  • html竖排改为横排
  • css文件放在什么目录
  • node的express
  • webservice规范
  • JavaScript中getUTCSeconds()方法的使用详解
  • jquery显示div
  • 基于jQuery的ajax方法封装
  • JAVAscript字符串类型单引号和双引号意一样吗
  • 1988年中华人民共和国印花税票五元
  • 深圳发票真伪查询入口
  • 远程视频监控设备
  • 广东地方税务局领导班子名单
  • 浙江网上税务局申报
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设