简体   繁体   English

更新XML类型列的值

[英]Update values of an XML type column

I have a problem! 我有个问题! We have an SQL Server table that has an XML type column. 我们有一个具有XML类型列的SQL Server表。 This column have a number of nodes and some of them have misspelled values. 该列有许多节点,其中一些节点的拼写错误。
I would like to write a script which updates all occurrences of this misspelling! 我想编写一个脚本来更新所有这种拼写错误! The problem is I don't know how to do it! 问题是我不知道该怎么做!
Let's say that one of the nodenames in the XML column is TEST and some of these nodes have the misspelled value MISSPELLED and I want it to be modified to CORRECT after the update. 假设XML列中的节点名称之一为TEST并且其中一些节点的拼写错误值为MISSPELLED ,我希望在更新后将其修改为CORRECT
Is this possible to do and if so how? 这可能吗?如果可以,怎么办?
Thanks in advance. 提前致谢。
/Kaunda /昆达

You can modify XML column data by following way : 您可以通过以下方式修改XML列数据:

CREATE TABLE Test (C1 XML)
INSERT INTO Test 
VALUES ('<REF>
            <Person>
                <FirstName>John</FirstName>
                <LastName>Smith</LastName>
                <System>
                    <User><Systemid>System1</Systemid><Type>UserId</Type><Useridentity>Userid1</Useridentity></User>
                    <User><Systemid>System2</Systemid><Type>UserIdx</Type><Useridentity>Userid2</Useridentity></User>
                    <User><Systemid>System2</Systemid><Type>Department</Type><Useridentity>Finance</Useridentity></User>
                    <User><Systemid>System3</Systemid><Type>UserId</Type><Useridentity>Userid3</Useridentity></User>
                    <User><Systemid>System4</Systemid><Type>UserIdx</Type><Useridentity>Userid4</Useridentity></User>
                </System>
            </Person>
        </REF>
')


UPDATE [Test]
SET C1.modify('replace value of (/REF/Person/System/User/Type/text())[1] with "UserId"')
WHERE C1.value('(/REF/Person/System/User/Type/text())[1]', 'varchar(max)') LIKE '%UserIdx%'

UPDATE : 更新:

For multiple values update in column, you need to write script like below : 对于列中的多个值更新,您需要编写如下脚本:

DECLARE @strSQL NVARCHAR(MAX)
DECLARE @Index INT
DECLARE @NodeCount INT

SET @Index = 1
SELECT @NodeCount = C1.value('count(/REF/Person/System/User/Type)', 'INT') from Test

WHILE @Index <= @NodeCount
BEGIN
    SET @strSQL = ' UPDATE [Test]
                    SET C1.modify(''replace value of (/REF/Person/System/User/Type/text())[' + CAST(@Index AS VARCHAR(10)) + '] with "UserId"'')
                    WHERE C1.value(''(/REF/Person/System/User/Type/text())[' + CAST(@Index AS VARCHAR(10)) + ']'', ''varchar(max)'') LIKE ''%UserIdx%'''

    EXEC(@strSQL)

    SET @Index = @Index + 1
END
<REF><Person><FirstName>John</FirstName><LastName>Smith</LastName><System><User><Systemid>System1</Systemid><Type>UserId</Type><Useridentity>Userid1</Useridentity></User><User><Systemid>System2</Systemid><Type>UserIdx</Type><Useridentity>Userid2</Useridentity></User><User><Systemid>System2</Systemid><Type>Department</Type><Useridentity>Finance</Useridentity></User><User><Systemid>System3</Systemid><Type>UserId</Type><Useridentity>Userid3</Useridentity></User><User><Systemid>System4</Systemid><Type>UserIdx</Type><Useridentity>Userid4</Useridentity></User></System></Person></REF>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM