SQL Server XML数据类型列中的XML搜索和更改

时间:2015-12-04 18:55:49

标签: sql-server xml tsql

我想在表中更新该表中所有记录的数据类型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>

我想将此更改为

<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>

基本改变是改变属性标识符的值=&#34; RESPONSE&#34;在<responseDeclation>标记中。

2 个答案:

答案 0 :(得分:1)

在XML类型列上使用modify method

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

上面的XQuery示例假设您的XML架构只出现一次assessmentItem和/或responseDeclaration

答案 1 :(得分:0)

如其他答案中所述,您可以使用SQL Server replace value of表达式来修改XML数据类型中的值。但它有局限性,一次只能更新一个值(参见文档:replace value of (XML DML) > Arguments > Expression1)。

需要注意的另一件事是您的XML数据具有默认命名空间。所以你需要告诉SQL Server的XQuery,例如:

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

对于演示,请考虑XML变量中的以下数据:

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>'

演示查询:

--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)')

输出(前一个是'之前的值',另一个是'之后'的值)

VH221999_order_match_choice_list_10.RESPONSE
RESPONSE