位置: 编程技术 - 正文

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

  • 个税手续费返还会计分录
  • 个人开具服务费发票税率
  • 自然人扣缴系统初次登录怎么设置密码
  • 有营业额有成本怎么算成本占比
  • 弥补以前年度亏损后怎么交所得税
  • 修缮服务开票项目一览表
  • 银行承兑汇票 托收
  • 净资本和净资产区别
  • 子母公司有连带责任吗
  • 交易性金融资产借贷方向
  • 土地增值税税收优惠
  • 企业卫生费是指什么
  • 一般纳税人计提增值税账务处理
  • 境外人员为境内企业提供劳务如何缴税
  • 出售使用过的生产设备
  • 分包工程款税率
  • 内部部门汇缴上级财务
  • 精创云所有者权益为负
  • 船运费发票抵扣多少税
  • 增值税普通发票有什么用
  • 三证合一地税号查询
  • 个人汽车租给公司租金需多少钱?
  • 到底如何理解参数方程
  • 年终奖的税收筹措是什么
  • 减税降费政策六税
  • 资本金结汇资金用途
  • 开票软件安装6位地区编号填什么
  • 工程收入怎么算
  • 利润的三个计算口径
  • 成本是否有含税金额
  • 借主营业务成本贷库存商品
  • 小规模增值税报表模板
  • 坏账准备递延所得税资产,不缴所得税
  • 临时工工资单怎么做
  • 国债逆回购会跌吗
  • 2021年windows最新版本
  • 给员工支付的房租怎么算
  • win11 应用商店
  • Win10无法访问局域网计算机
  • 有留抵税额怎么做分录
  • 废旧物资回收企业所得税优惠政策
  • 将自产的应税消费品用于连续生产应税消费品
  • php框架symfony
  • 关于企业财务会计的说法正确的是
  • thinkphp框架介绍
  • vue项目使用rem
  • 进项税额转出结转会计分录怎么写
  • 个体户一直未申报会怎样
  • 接受非货币性资产投资会计处理
  • java中的常量是什么意思
  • 企业员工福利包括哪些内容
  • 小规模纳税人现代服务税率
  • mysqldump定时备份
  • 土地免缴土地使用税
  • 扫码开票软件
  • 企业需要建立什么?完善什么?
  • 宣传费怎么做账
  • 产品检测费计入什么二级科目里
  • 企业要怎样才能用到便宜的天然气
  • 企业优惠政策是什么意思
  • 新准则固定资产处置的账务处理
  • 银行汇票的适用主体
  • xp系统新建用户后原来的用户没有了
  • win10系统经常弹出广告页面怎么解决
  • 下载的mt4安装成了mt5
  • Linux使用命令行将一个文件夹移动到另一个文件夹
  • Javascript之String对象详解
  • extjs grid设置某列背景颜色和字体颜色的实现方法
  • 学习标准——笔记
  • vs提示图标的含义
  • Node.js中的construct
  • node.js中的http.response.end方法使用说明
  • mongoose操作
  • JavaScript中的数据类型
  • javascript语言基础
  • unity与android交互详细
  • 使用jQuery给input标签设置默认值
  • 光伏发电要交税不
  • 百旺税控盘电话服务热线
  • 小规模纳税人应交增值税账务处理
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设