簡體   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