Sql server sqlserver中的XML问题

Sql server sqlserver中的XML问题,sql-server,xml,sql-server-2005,Sql Server,Xml,Sql Server 2005,在我的一个sql脚本中,我需要使用以下xml字符串执行一个存储过程 <Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Field> <Attributes> <Attribute Name="CODE1" IsRequired="tr

在我的一个sql脚本中,我需要使用以下xml字符串执行一个存储过程

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Field>
        <Attributes>
            <Attribute Name="CODE1" IsRequired="true" Order="1" IsVisible="true"/>
            <Attribute Name="CODE2" IsRequired="true" Order="2" IsVisible="true"/>
        </Attributes>
        <Rows>
            <Row ProductState="5">
                <Items>
                    <Item Name="PROD1" SendCustomer="false"/>
                    <Item Name="PROD2" SendCustomer="false"/>
                </Items>
            </Row>
        </Rows>
    </Field>
</Collection>

我从不同的表中获取
属性
信息。我正在编写一个通用函数,在该函数中传递一个ID并返回这个XML字符串,SQL脚本使用该字符串执行存储过程

有时,我需要覆盖某些元素的属性值,如
SendCustomer
。我最初的想法是将其反序列化为临时表,用覆盖值更新临时表,然后将其序列化回XML

因此,从本质上讲,整个过程归结为:

  • 查询表,在函数中序列化为XML
  • 反序列化XML,存储在临时表中
  • 如有必要,覆盖值
  • 再次将表序列化为XML

  • sql server 2005中是否有更优雅的方法来完成整个过程?

    实际上可以使用XQuery修改XML数据类型。看

    declare@xxml;
    选择@x=N'
    ';
    设置@x.modify(不替换
    (/Collection/Field/Rows/Row/Items/Item[@Name=“PROD2”]/@SendCustomer)[1]
    以"真"字表示;;
    选择@x;
    
    您能解释一下JQuery解决方案如何适合我的问题吗?谢谢什么是JQuery?我的答案是Transact-SQL。
    declare @x XML;
    select @x = N'<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Field>
            <Attributes>
                    <Attribute Name="CODE1" IsRequired="true" Order="1" IsVisible="true"/>
                    <Attribute Name="CODE2" IsRequired="true" Order="2" IsVisible="true"/>
            </Attributes>
            <Rows>
                    <Row ProductState="5">
                            <Items>
                                    <Item Name="PROD1" SendCustomer="false"/>
                                    <Item Name="PROD2" SendCustomer="false"/>
                            </Items>
                    </Row>
            </Rows>
        </Field>
    </Collection>';
    
    set @x.modify(N'replace value of 
        (/Collection/Field/Rows/Row/Items/Item[@Name="PROD2"]/@SendCustomer)[1]
        with "true"');
    
    select @x;