How to update the existing node value of XML by multiplying it with n times in SQL Server

末鹿安然 提交于 2019-12-11 09:00:35

问题


Here is the XML I have in my Table Field

<CtcConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Ctc>3</Ctc>
    <SalaryComponent>
        <SalaryComponentConfiguration>
            <Name>Basic</Name>
            <DisplayOrder>0</DisplayOrder>
            <Value>5634655</Value>
        </SalaryComponentConfiguration>
        <SalaryComponentConfiguration>
            <Name>HR</Name>
            <DisplayOrder>0</DisplayOrder>
            <Value>1234</Value>
        </SalaryComponentConfiguration>
        <SalaryComponentConfiguration>
            <Name>medical</Name>
            <DisplayOrder>0</DisplayOrder>
            <Value>0</Value>
        </SalaryComponentConfiguration>
    </SalaryComponent>
</CtcConfiguration>

I want the update the existing node(DisplayOrder) value by multiplying it with n times.

This is what I get so far to update a node value:

    DECLARE @NodeName VARCHAR(100)=N'Basic';
    DECLARE @NewValue INT=3;
    UPDATE payroll.pays 
    SET CtcConfiguration.modify(
          N'replace value of (/CtcConfiguration
                              /SalaryComponent
                              /SalaryComponentConfiguration[(Name/text())[1]=sql:variable("@NodeName")]
                              /Value/text())[1] 
            with sql:variable("@NewValue")');

回答1:


I want to offer you two approaches:

Use these variables to pick the right node and to define the multiplier

DECLARE @AttributeName VARCHAR(100)=N'medical';
DECLARE @Multiply INT=2;

UPDATE YourTable
SET YourXML.modify(N'replace value of (/CtcConfiguration
                                       /SalaryComponent
                                       /SalaryComponentConfiguration[(Name/text())[1]=sql:variable("@AttributeName")]
                                       /DisplayOrder/text())[1] 
                     with xs:int((/CtcConfiguration
                                 /SalaryComponent
                                 /SalaryComponentConfiguration[(Name/text())[1]=sql:variable("@AttributeName")]
                                 /DisplayOrder/text())[1]) * sql:variable("@Multiply")');

--Alternatively you can use an updateable CTE:
--In this case you use sql:column() instead of sql:variable()

SET @AttributeName=N'Basic';

WITH cte AS
(
    SELECT *
           --you can place any multiplier here
          ,10 * YourXML.value(N'(/CtcConfiguration
                                 /SalaryComponent
                                 /SalaryComponentConfiguration[(Name/text())[1]=sql:variable("@AttributeName")]
                                 /DisplayOrder/text())[1] ',N'int') AS NewValue
    FROM YourTable
)
UPDATE cte
SET YourXML.modify(N'replace value of (/CtcConfiguration
                                       /SalaryComponent
                                       /SalaryComponentConfiguration[(Name/text())[1]=sql:variable("@AttributeName")]
                                       /DisplayOrder/text())[1] 
                     with sql:column("NewValue")');


来源:https://stackoverflow.com/questions/50695380/how-to-update-the-existing-node-value-of-xml-by-multiplying-it-with-n-times-in-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!