位置: 编程技术 - 正文

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

  • 商誉是减值还是摊销
  • 个人独资企业需要监事吗
  • 利润分配会计处理例题及解析
  • 资产负债表怎么看营业收入
  • 六税两费减半征收政策
  • 缴注册印花税怎么做账
  • 员工超过万人公司
  • 营改增后企业所得税怎么算
  • 负债类科目有借方余额吗
  • 河道工程修建维护管理费何时开始停征?
  • 自己生产的产品用于职工福利
  • 公司员工体检计入什么科目
  • 内账价税分离余额怎么处理
  • 小规模购买财务软件怎么做分录
  • 股东其他应付款可以转为实收资本文本格式
  • 公司没有发票的做内账,那么支付款从哪里来
  • 怎么让资产负债率下降
  • 出差补贴算工资薪金吗
  • win10系统如何禁用驱动强制签名
  • linux attached
  • 开增值税专用发票必须走公账吗
  • php sql 教程
  • 清算公司资产怎么入账
  • .exe是什么文件
  • 公司名称变更重新刻章
  • 无偿划转股权
  • php date_diff
  • ros 2
  • 混合销售行为的税务处理
  • 新政府会计制度科目解读
  • yolov5源码讲解
  • 公司破产清算应先还银行还是股东
  • 企业计提的医疗保险属于企业设定提存计划的内容
  • 存货盘盈盘亏的会计处理
  • 应付职工薪酬属于什么会计科目
  • 业务出差餐费如何记账
  • 跨年的发票可以退税吗
  • 交易性金融资产是什么意思
  • 冲红的发票怎么做账会计分录
  • 有外币业务需不需要交税
  • 其他应收款主要核算什么
  • 制作海报属于什么行业
  • SQLServer 2008 Merge语句的OUTPUT功能
  • 经审计的财务报表是否要会计师事务所盖章
  • 营改增之后建筑业预收账款是否需要预交税款
  • 其他应收款如何计提减值准备
  • 托收承付和委托收款纳税义务发生时间
  • 接受捐赠做账
  • 研发费用是否包含增值税
  • 零申报报表怎么填写
  • 去年多计提的费用
  • 损益类会计科目有哪些
  • 其他业务支出可以在企业所得税前扣除吗
  • 出口转内销补交进口增值税时间
  • 财务费用在贷方负数表示什么
  • 新成立公司怎么办理社保开户
  • 一个身份证可以实名几个qq
  • mysql存文章的字段设置为多大?
  • win2003peiso
  • 如何用u盘装系统 知乎
  • mscorsvw.exe是什么
  • 苹果mac系统怎么用
  • win7系统怎么禁止软件自启动
  • win8 恢复
  • xp系统设置局域网
  • win8 侧边栏
  • win8磁盘分区
  • 卡带测评
  • 新浪微博随时随地
  • 如何检测ip地址是否被占用
  • python tkinter tabview
  • android设计模式书籍
  • android 发短信
  • python+flask
  • 广东电子税务局app
  • 国税干部任前谈心谈话
  • 税收征管工作的基本目标
  • 新市税务所电话
  • 印花税怎么计提和结转
  • 税务部门放管服
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设