問題描述
我想知道如何替換存儲(chǔ)在 SQL Server 數(shù)據(jù)庫中的 xml 中的子節(jié)點(diǎn)名稱
I'd like to know how I can replace a child node name in a xml that I stored in my SQL Server database
示例 XML
<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
<Test_Node>Yes</Test_Node>
</ProductionServers>
</ProductionServer>
</CompanyStatus>
我將如何將其更改為以下內(nèi)容:
How would I change that to the following:
<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
<Live_Node>Yes</Live_Node>
</ProductionServers>
</ProductionServer>
</CompanyStatus>
基本上唯一的變化是
被重命名為
但值是相同的.
Where essentially the only change is <Test_Node>
is renamed to <Live_Node>
but the value is the same.
有沒有簡單的方法可以做到這一點(diǎn)?
Is there a simple way to do this?
我的數(shù)據(jù)庫中有大約 1000 條記錄
I have about 1000 records in my database
推薦答案
這是我的建議
- 使用屬性保存
- 容忍元素的位置(只要這個(gè)元素是唯一的)
檢查一下:
DECLARE @xml XML=
N'<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
<Test_Node a="x" b="y" c="z">Yes</Test_Node>
</ProductionServer>
</ProductionServers>
</CompanyStatus>';
--這將創(chuàng)建帶有新元素名稱
的
及其所有屬性(如果有):
--This will create the <Test_Node>
with all its attributes (if there are any) with the new element name <Live_Node>
:
DECLARE @NewNode XML=
(
SELECT @xml.query(N'let $nd:=(//*[local-name()="Test_Node"])[1]
return
<Live_Node> {$nd/@*}
{$nd/text()}
</Live_Node>
')
);
--這將首先在原始之后直接插入"@NewNode"
,并將刪除原始:
--this will first insert the "@NewNode"
directly after the original, and will remove the original:
SET @xml.modify(N'insert sql:variable("@NewNode") after (//*[local-name()="Test_Node"])[1]');
SET @xml.modify(N'delete (//*[local-name()="Test_Node"])[1]');
SELECT @xml;
結(jié)果
<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
<Live_Node a="x" b="y" c="z">Yes</Live_Node>
</ProductionServer>
</ProductionServers>
</CompanyStatus>
更新:與使用可更新 CTE 的表格數(shù)據(jù)相同:
DECLARE @xmlTable TABLE (YourXml XML);
INSERT INTO @xmlTable VALUES
(--Test_Node has got attributes
N'<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
<Test_Node a="x" b="y" c="z">Yes</Test_Node>
</ProductionServer>
</ProductionServers>
</CompanyStatus>'
)
,( --different position, no attributes
N'<CompanyStatus>
<ProductionServers>
<Test_Node>Yes</Test_Node>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
</ProductionServer>
</ProductionServers>
</CompanyStatus>'
)
,( --No test node at all
N'<CompanyStatus>
<ProductionServers>
<ProductionServer>
<Patch>0</Patch>
<Status>Green</Status>
</ProductionServer>
</ProductionServers>
</CompanyStatus>'
);
--可更新的 CTE 返回原始節(jié)點(diǎn)和新節(jié)點(diǎn).這可以一次性更新:
--the updateable CTE returns the original and the new node. This can be updated in one go:
WITH ReadNode AS
(
SELECT t.YourXml.query(N'let $nd:=(//*[local-name()="Test_Node"])[1]
return
<Live_Node> {$nd/@*}
{$nd/text()}
</Live_Node>
') AS NewNode
,t.YourXml AS Original
FROM @xmlTable AS t
)
UPDATE ReadNode SET Original.modify(N'insert sql:column("NewNode") after (//*[local-name()="Test_Node"])[1]');
UPDATE @xmlTable SET YourXml.modify(N'delete (//*[local-name()="Test_Node"])[1]');
SELECT *
FROM @xmlTable
這篇關(guān)于替換存儲(chǔ)在 SQL Server 數(shù)據(jù)庫列中的 XML 中的節(jié)點(diǎn)名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!