簡體   English   中英

如何在SQL Server的同一張表中顯示XML節點內容從一列到另一列?

[英]How can I display XML node content from one column into another column on the same table in SQL Server?

一列包含整個XML正文。 我想采用該XML並從特定節點獲取內容,並將其放入同一表的另一列中。 在SQL Server中執行此操作的最佳方法是什么?

我嘗試使用計算列,但不確定如何創建公式來隔離單個XML節點。

謝謝!

像這樣嗎

DECLARE @tbl TABLE (ID INT IDENTITY, BodyXML XML, ContentXML XML, MergedXML XML);
INSERT INTO @tbl VALUES
 (N'<root>
    <!-- This can be a fully blown document, we want to insert into "inner node" -->
    <innerNode>
    </innerNode>
    </root>'
 ,N'<root>
    <Content attr="yes">This we want to embed</Content>
    <Content attr="no">this not</Content>
    <Content attr="yes">but this</Content>
    </root>'
  ,NULL);

UPDATE @tbl SET MergedXML=BodyXML; --pre set all with the body

WITH updateableCTE AS
(
    SELECT MergedXML AS TargetColumn
          ,ContentXML.query('/root/Content[@attr="yes"]') AS EmbedThis 
    FROM @tbl 
)
UPDATE updateableCTE SET TargetColumn.modify(N'insert sql:column("EmbedThis") into (/root/innerNode)[1]');

SELECT * FROM @tbl; 

這是新的“ MergedXML”:

<root>
  <!-- This can be a fully blown document, we want to insert into "inner node" -->
  <innerNode>
    <Content attr="yes">This we want to embed</Content>
    <Content attr="yes">but this</Content>
  </innerNode>
</root>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM