在我的一个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>
我从不同的表中获取Attribute
和Item
信息。我正在编写一个泛型函数,您可以在其中传递一个ID并返回此SQL字符用于执行存储过程的XML字符串
有时,我需要覆盖某些元素的属性值,例如SendCustomer
。我最初的想法是将其反序列化为临时表,使用覆盖值更新临时表,然后将其序列化为XML。
因此,基本上,整个过程归结为:
在sql server 2005中有更优雅的方法来完成整个过程吗?
答案 0 :(得分:1)
实际上可以使用XQuery修改XML数据类型。请参阅the modify() method。
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;