0

I have XML in one of the column in the XYZ table, and I need to update the Amount element with a new value instead of 0.00, and the PolicyReference and AccountReference elements with two different values instead of blank.

For example:

<PolicyReference>7657576567</PolicyReference>
<AccountReference>7657576875</AccountReference>

This is my XML in the column :

<document>
    <StatusCode>ACV</StatusCode>
    <PaymentMethodDetail>
        <EFT>
            <AccountNumber>123456789</AccountNumber>
            <AccountName>ABCDEFGHIJK</AccountName>
        </EFT>
    </PaymentMethodDetail>
    <PaymentExtendedData>
        <CHECK>
            <Source>System</Source>
            <SourceType>ACH</SourceType>
        </CHECK>
    </PaymentExtendedData>
    <PostMarkDate />
    <EntryUserId>1</EntryUserId>
    <Amount>0.00</Amount>
    <EntryDate />
    <PolicyLineOfBusiness>LOL</PolicyLineOfBusiness>
    <PolicyReference />
    <AccountReference />
    <AccountId>2034001793</AccountId>
</document>

This is what I have tried:

UPDATE XYZ
SET XmlPayload.modify('replace value of (//document/PolicyReference/)[1] with "<PolicyReference>275654</PolicyReference>"')
WHERE PaymentSearchId = 18785

I am getting an error:

Msg 9341, Level 16, State 1, Line 4
XQuery [XYZ.XmlPayload.modify()]: Syntax error near ')', expected a step expression

3
  • What have you tried? Are you getting errors? Commented Oct 2, 2020 at 13:05
  • UPDATE XYZ SET XmlPayload.modify(' + '''replace value of(//document/PolicyReference/text())[1] with "' <PolicyReference> + 546545 + </PolicyReference> '") PaymentReferenceNumber = ''' 546545 ''' Commented Oct 2, 2020 at 16:12
  • Its not working, please correct me what I am doing mistake here Commented Oct 2, 2020 at 16:12

1 Answer 1

2

I think this is a good question as it presents an interesting challenge of having an existing element without a text value. This is handled differently than simply adding a new element or replacing the contents of an existing one.

First, though, your provided XML was broken. If that is the XML you're receiving, you have other issues. For instance, in your original question, you had </AccountReference> which is invalid syntax by itself. I corrected this to <AccountReference /> both in your question as well as in my example.

With empty XML elements, you need to call the insert text DML of the XML.modify method.

DECLARE @xml XML = 
'<document>
    <StatusCode>ACV</StatusCode>
    <PaymentMethodDetail>
       <EFT>
          <AccountNumber>123456789</AccountNumber>
          <AccountName>ABCDEFGHIJK</AccountName>
       </EFT>
    </PaymentMethodDetail>
    <PaymentExtendedData>
       <CHECK>
          <Source>System</Source>
          <SourceType>ACH</SourceType>
        </CHECK>
    </PaymentExtendedData>
    <PostMarkDate />
    <EntryUserId>1</EntryUserId>
    <Amount>0.00</Amount>
    <EntryDate />
    <PolicyLineOfBusiness>LOL</PolicyLineOfBusiness>
    <PolicyReference />
    <AccountReference />
    <AccountId>2034001793</AccountId>
</document>';

DECLARE 
    @Amount DECIMAL(18,2) = 99.95,
    @AccountReference VARCHAR(20) = '7657576875',
    @PolicyReference VARCHAR(20) = '7657576567';

/* Update Amount */
SET @xml.modify('
    replace value of (/document/Amount/text())[1]
    with sql:variable("@Amount")
');

/* Insert the AccountReference text */
SET @xml.modify('
    insert text {sql:variable("@AccountReference")} into (/document/AccountReference[1])[1]
');

/* Insert the PolicyReference text */
SET @xml.modify('
    insert text {sql:variable("@PolicyReference")} into (/document/PolicyReference[1])[1]
');

/* Show updated XML */
SELECT @xml;

The updated XML is now:

<document>
  <StatusCode>ACV</StatusCode>
  <PaymentMethodDetail>
    <EFT>
      <AccountNumber>123456789</AccountNumber>
      <AccountName>ABCDEFGHIJK</AccountName>
    </EFT>
  </PaymentMethodDetail>
  <PaymentExtendedData>
    <CHECK>
      <Source>System</Source>
      <SourceType>ACH</SourceType>
    </CHECK>
  </PaymentExtendedData>
  <PostMarkDate />
  <EntryUserId>1</EntryUserId>
  <Amount>99.95</Amount>
  <EntryDate />
  <PolicyLineOfBusiness>LOL</PolicyLineOfBusiness>
  <PolicyReference>7657576567</PolicyReference>
  <AccountReference>7657576875</AccountReference>
  <AccountId>2034001793</AccountId>
</document>

An example of updating a table:

DECLARE @xyz TABLE ( PaymentSearchId INT, XmlPayload XML );
INSERT INTO @xyz VALUES ( 18785, 
'<document>
    <StatusCode>ACV</StatusCode>
    <PaymentMethodDetail>
       <EFT>
          <AccountNumber>123456789</AccountNumber>
          <AccountName>ABCDEFGHIJK</AccountName>
       </EFT>
    </PaymentMethodDetail>
    <PaymentExtendedData>
       <CHECK>
          <Source>System</Source>
          <SourceType>ACH</SourceType>
        </CHECK>
    </PaymentExtendedData>
    <PostMarkDate />
    <EntryUserId>1</EntryUserId>
    <Amount>0.00</Amount>
    <EntryDate />
    <PolicyLineOfBusiness>LOL</PolicyLineOfBusiness>
    <PolicyReference />
    <AccountReference />
    <AccountId>2034001793</AccountId>
</document>' );

DECLARE 
    @PaymentSearchId INT = 18785,
    @Amount DECIMAL(18,2) = 99.95,
    @AccountReference VARCHAR(20) = '7657576875',
    @PolicyReference VARCHAR(20) = '7657576567';

/* Update Amount */
UPDATE @xyz
SET XmlPayload.modify('
    replace value of (/document/Amount/text())[1]
    with sql:variable("@Amount")
')
WHERE PaymentSearchId = @PaymentSearchId;

/* Insert the AccountReference text */
UPDATE @xyz
SET XmlPayload.modify('
    insert text {sql:variable("@AccountReference")} into (/document/AccountReference[1])[1]
')
WHERE PaymentSearchId = @PaymentSearchId;

/* Insert the PolicyReference text */
UPDATE @xyz
SET XmlPayload.modify('
    insert text {sql:variable("@PolicyReference")} into (/document/PolicyReference[1])[1]
')
WHERE PaymentSearchId = @PaymentSearchId;

/* Show updated XML */
SELECT XmlPayload FROM @xyz WHERE PaymentSearchId = @PaymentSearchId;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.