您确实有几个选择,但可能不会比您的三个更新好多少。选项是 1) 重建 XML 或 2) 创建映射表。
XML 上的 DML 操作代价高昂,因此在重构 XML 时有一个临界点可能会更有效 - 取决于 XML 的大小、要替换的属性数量等
映射表解决方案使用循环,因此类似于三更新解决方案。但是,您确实具有使其成为数据驱动的优势。
无论如何,看看这个演示,让我知道你的进展情况:
USE tempdb
GO
SET NOCOUNT ON
DECLARE @t TABLE ( yourXML XML )
INSERT INTO @t ( yourXML )
SELECT '<ENTITY NAME="entity1">
<ATTR ID="attr1" CAPTION="Attributes to Change" SIZE="100" WIDTH="100"></ATTR>
</ENTITY>'
SELECT 'before' s, * FROM @t
------------------------------------------------------------------------------------------------
-- Reconstruct the inner xml START
------------------------------------------------------------------------------------------------
-- Delete current element and attributes
UPDATE @t
SET yourXML.modify('delete ENTITY[@NAME="entity1"]/ATTR' )
DECLARE @xml XML = '', @caption VARCHAR(100), @size INT, @width INT
SELECT @caption = 'New Attribute Caption', @size = 200, @width = 300
-- Construct new element with attributes
SET @xml = @xml.query ( '<ATTR ID="attr1" CAPTION="{sql:variable("@caption")}" SIZE="{sql:variable("@size")}" WIDTH="{sql:variable("@width")}"></ATTR> ' )
-- Insert it back in
UPDATE t
SET yourXML.modify('insert sql:variable("@xml") into (ENTITY[@NAME="entity1"])[1] ')
FROM @t t
SELECT 'after' s, * FROM @t
-- Reconstruct the inner xml END
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-- Create mapping table and loop START
------------------------------------------------------------------------------------------------
DECLARE @i INT = 0
DECLARE @map TABLE ( attributeName VARCHAR(50) PRIMARY KEY, newValue VARCHAR(50) NOT NULL )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'CAPTION', 'New Attribute Caption 3' )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'SIZE', 123 )
INSERT INTO @map ( attributeName, newValue ) VALUES ( 'WIDTH', 456 )
SELECT 'before 2' s, * FROM @t
WHILE 1=1
BEGIN
SET @i += 1
-- Update the XML based on the mapping table
UPDATE @t
SET yourXML.modify('replace value of (/ENTITY/ATTR/@*[local-name()= sql:column("attributeName")])[1] with sql:column("newValue")')
FROM @t
CROSS JOIN @map m
WHERE yourXML.exist('(/ENTITY/ATTR/@*[local-name()= sql:column("attributeName")][. != sql:column("newValue")])') = 1
IF @@rowcount = 0 BREAK
-- Guard against infinite loop
IF @i > 99 BEGIN RAISERROR( 'Too many loops! %i', 16, 1, @i ) BREAK END
END
SELECT @i loops
SELECT 'after' s, * FROM @t
-- Create mapping table and loop END
------------------------------------------------------------------------------------------------