如何从数据库中的 XML 列中删除节点
How to delete node from XML column in DB
我正在尝试从 SQL Table 的 XML 列中删除特定“节点”。
以下是 XML 列内容之一的示例。
<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>
我正在尝试删除此处的重复记录 - 例如“178.160.245.88”
我一直在尝试“删除”语句的多种变体 - 请在这方面提供一些帮助。
set column.modify('delete /GodBrandConfig/AllowableIpAddresses/"178.160.245.88")') where idcolumn= 1125;
您需要在您的 XPath 中 select 一个 AllowableIpAddress
节点,并且由于您想要删除重复项,您可以删除与指定文本匹配的第二个匹配项,例如:
update #DemoTable
set [column].modify('delete /GodBrandConfig/AllowableIpAddresses/AllowableIpAddress[text()="178.160.245.88"][2]')
where idcolumn = 1125;
这会产生更新后的 XML:
<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>
检查这是否符合您的需要
DECLARE @myDoc XML
SET @myDoc = '<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>'
-- Create clean node without duplication using "distinct-values"
DECLARE @AllowableIpAddress XML
SELECT @AllowableIpAddress = (
SELECT @myDoc.query('
<AllowableIpAddresses>
{
for $x in distinct-values(//AllowableIpAddress/text())
return <AllowableIpAddress>{$x}</AllowableIpAddress>
}
</AllowableIpAddresses>
')
)
-- "replace node" is not supported in T-SQL (it is supported in oracle)
-- Therefore I will use modify with the actions "delete" and "insert"
SET @myDoc.modify('delete (/GodBrandConfig/AllowableIpAddresses)')
SET @myDoc.modify('insert sql:variable("@AllowableIpAddress") after (/GodBrandConfig/WebClientUrl)[1]')
SELECT @myDoc ;
我正在尝试从 SQL Table 的 XML 列中删除特定“节点”。 以下是 XML 列内容之一的示例。
<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>
我正在尝试删除此处的重复记录 - 例如“178.160.245.88”
我一直在尝试“删除”语句的多种变体 - 请在这方面提供一些帮助。
set column.modify('delete /GodBrandConfig/AllowableIpAddresses/"178.160.245.88")') where idcolumn= 1125;
您需要在您的 XPath 中 select 一个 AllowableIpAddress
节点,并且由于您想要删除重复项,您可以删除与指定文本匹配的第二个匹配项,例如:
update #DemoTable
set [column].modify('delete /GodBrandConfig/AllowableIpAddresses/AllowableIpAddress[text()="178.160.245.88"][2]')
where idcolumn = 1125;
这会产生更新后的 XML:
<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>
检查这是否符合您的需要
DECLARE @myDoc XML
SET @myDoc = '<GodBrandConfig>
<AppSecret>hello</AppSecret>
<WebClientUrl>url</WebClientUrl>
<AllowableIpAddresses>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>.*</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>178.160.245.88</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
<AllowableIpAddress>000.000.000.000</AllowableIpAddress>
</AllowableIpAddresses>
<GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>'
-- Create clean node without duplication using "distinct-values"
DECLARE @AllowableIpAddress XML
SELECT @AllowableIpAddress = (
SELECT @myDoc.query('
<AllowableIpAddresses>
{
for $x in distinct-values(//AllowableIpAddress/text())
return <AllowableIpAddress>{$x}</AllowableIpAddress>
}
</AllowableIpAddresses>
')
)
-- "replace node" is not supported in T-SQL (it is supported in oracle)
-- Therefore I will use modify with the actions "delete" and "insert"
SET @myDoc.modify('delete (/GodBrandConfig/AllowableIpAddresses)')
SET @myDoc.modify('insert sql:variable("@AllowableIpAddress") after (/GodBrandConfig/WebClientUrl)[1]')
SELECT @myDoc ;