繁体   English   中英

使用SQL在特定位置插入特定XML节点

[英]Insert specific XML node in specific location using SQL

我正在寻找一种在特定位置插入特定XML节点的方法。 下面是示例:

<Car>
     <Brand>Toyota</Brand>
     <Color>Red</Color>
     <Price>10000</Color>
</Car>

假设我要在<Color><Price>节点之间插入一个<Year>2012</Year> <Price>节点,该怎么办?

请注意,在当前情况下,替换整个XML文档不是一个选择。 谢谢

您可以使用XQuery insert ... after ...语句在Color之后插入Year元素:

declare @xml XML = '<Car>
     <Brand>Toyota</Brand>
     <Color>Red</Color>
     <Price>10000</Price>
</Car>'

SET @xml.modify('
    insert <Year>2012</Year>
    after (//Color)[1]
')

SELECT @xml

输出:

<Car>
  <Brand>Toyota</Brand>
  <Color>Red</Color>
  <Year>2012</Year>
  <Price>10000</Price>
</Car>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM