位置: 编程技术 - 正文

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

发布时间:2024-01-30
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安装方法)

  • 信息化投入包括手机吗
  • 纳税申报操作视频
  • 农产品减免税政策最新
  • 税控盘抄报逾期怎么办
  • 增值税少企业所得税多说明什么
  • 开票打印机可以自己买吗
  • 个人公积金计入什么会计科目
  • 机耕道属于水利还是土地整治
  • 一般纳税人资格证书是什么样的
  • 研发投入和研发支出的关系
  • 哪些收据可以税务登记
  • 先预付一部分货款分录
  • 异地缴纳个人所得税
  • 文化事业建设费的征收范围
  • 2016年的发票2020年可以认证吗
  • 普票红冲后原件没有了怎么办
  • 商贸公司购买货物会计分录
  • 费用报销单的摘要是什么意思
  • 盈余公积补亏影响资产吗
  • 租赁固定资产折旧计入
  • 平销返利可以作为下期的折扣开票吗?
  • 申报个税是按哪个月的工资表
  • 用盈余公积弥补亏损会影响所有者权益总额吗
  • 利润表中的其他综合收益
  • mac复制文件路径后怎么粘贴
  • 利润分配怎么核算
  • 稿酬所得怎么纳税
  • 收到个人款开公司发票
  • mac怎么同步
  • 工程长期停工
  • 此设备某些文件可能存在问题
  • 安徽4岁男孩户口已解决
  • 柏林亚历山大广场1980
  • 企业低值易耗品摊销计入产品成本的方法
  • 小微企业免征增值税政策2023
  • php -r
  • yolov3原理及代码解析
  • php分页技术
  • extrim命令详解
  • 收回已核销的坏账并入账是什么意思
  • 初级会计直线法摊销
  • 500元以下开收据要交税吗
  • 预收账款的借贷方向是加还是凿
  • 普通发票可以抵扣进项吗
  • SQL server 2008安装程序遇到以下错误 sku
  • 现金支出的账务处理
  • 已抵扣发票在什么地方查
  • 营业额包括成本价吗
  • 个税汇算清缴时劳务报酬怎么计税
  • 发生销售退回账务处理
  • 解除劳动合同的合法程序
  • 房地产企业扣除项目
  • 房地产预交税金怎么算
  • 旅行社开的发票如何记账?
  • 商品进销存台账
  • 此数据库中不存在用户或角色
  • icloud win10
  • server2003定时任务
  • 远程桌面登录显示其他用户
  • fedora os
  • linux中more命令详解
  • vmstat命令
  • windows手动启动服务
  • win7界面比win10好看
  • mac未能分区
  • windows mobile应用商店
  • win7电脑能玩啥游戏
  • linuxone
  • ftp上传网站的步骤
  • 编写批处理
  • jQuery position() 函数详解以及jQuery中position函数的应用
  • javascript总结笔记
  • js获取上传文件的大小
  • JavaScript+html5 canvas绘制缤纷多彩的三角形效果完整实例
  • Android自定义系统服务框架
  • 携程怎么订
  • 赣州市民中心上班时间电话号码
  • 源泉扣缴通俗
  • 个人无偿捐赠增值税
  • 福建原盐和自然盐有什么区别
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号