0

I would like to update a column of datatype XML in a table for all the records in that table.

For example below is the content of XML for a single row:

 <assessmentItem xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2" 
        adaptive="false" identifier="VHXYZ" timeDependent="false" 
        title="ZonesSS" toolName="IBIS Export" toolVersion="1.0">
    <responseDeclaration baseType="identifier" cardinality="single" 
             identifier="VH221999_order_match_choice_list_10.RESPONSE">
        <correctResponse>
            <value>i2</value>
        </correctResponse>
    </responseDeclaration>
    <outcomeDeclaration baseType="float" cardinality="single" identifier="SCORE">
        <defaultValue>
            <value>0</value>
        </defaultValue>
    </outcomeDeclaration>
</assessmentItem>

I want to change this to

<assessmentItem xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2"  
       adaptive="false" identifier="VHXYZ" timeDependent="false" title="ZonesSS" 
       toolName="IBIS Export" toolVersion="1.0">
    <responseDeclaration baseType="identifier" cardinality="single" identifier="RESPONSE">
        <correctResponse>
            <value>i2</value>
        </correctResponse>
    </responseDeclaration>
    <outcomeDeclaration baseType="float" cardinality="single" identifier="SCORE">
        <defaultValue>
            <value>0</value>
        </defaultValue>
    </outcomeDeclaration>
</assessmentItem>

Basic change is change the value of attribute identifier="RESPONSE" in <responseDeclation> tag.

2 Answers 2

1

Use the modify method on your XML type column.

update [YourTable]
set [XmlColumn].modify('replace value of
    (/assessmentItem/responseDeclaration/@identifier)[1] with "RESPONSE"'
)
where /*your where criteria here*/

The above XQuery example assumes your XML schema has only one occurrence of assessmentItem and/or responseDeclaration

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you . What if we have one more <responseDeclaration> and want to change its "Identifier" value then does the below query works? update [YourTable] set [XmlColumn].modify('replace value of (/assessmentItem/responseDeclaration/@identifier)[2] with "RESPONSE"' ) where /*your where criteria here*/
0

As mentioned in the other answer, you can use SQL Server replace value of expression to modify a value in XML data type. It has limitation though, that only a single value can be updated at a time (see the documentation : replace value of (XML DML) > Arguments > Expression1).

Another thing to note is that your XML data has default namespace. So you need to tell SQL Server's XQuery about that, for example :

declare default element namespace "http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2";

For demo, consider the following data in XML variable :

declare @xml XML = '<assessmentItem xmlns="http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2" 
        adaptive="false" identifier="VHXYZ" timeDependent="false" 
        title="ZonesSS" toolName="IBIS Export" toolVersion="1.0">
    <responseDeclaration baseType="identifier" cardinality="single" 
             identifier="VH221999_order_match_choice_list_10.RESPONSE">
        <correctResponse>
            <value>i2</value>
        </correctResponse>
    </responseDeclaration>
    <outcomeDeclaration baseType="float" cardinality="single" identifier="SCORE">
        <defaultValue>
            <value>0</value>
        </defaultValue>
    </outcomeDeclaration>
</assessmentItem>'

Demo Query :

--value before
;with xmlnamespaces(default 'http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2')
select @xml.value('(/assessmentItem/responseDeclaration/@identifier)[1]','varchar(max)')

--update operation
set @xml.modify('
declare default element namespace "http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2";
replace value of
    (/assessmentItem/responseDeclaration/@identifier)[1] with "RESPONSE"'
)

--value after
;with xmlnamespaces(default 'http://www.imsglobal.org/xsd/apip/apipv1p0/qtiitem/imsqti_v2p2')
select @xml.value('(/assessmentItem/responseDeclaration/@identifier)[1]','varchar(max)')

output : (the top one is the 'value before' and the other is the 'value after')

VH221999_order_match_choice_list_10.RESPONSE
RESPONSE

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.