1

我正在尝试更新Qty存储在 SQL Server XML 列中的 XML 节点,没有发生错误,但我的字段没有得到更新。

请指教。谢谢。

从 SQL Server xml 列复制的 XML 数据:

<ArrayOfCampaignVoucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <CampaignVoucher>
   <VouCode>Vouch002</VouCode>
   <Qty>4</Qty>
</CampaignVoucher>
<CampaignVoucher xsi:nil="true" /></ArrayOfCampaignVoucher>

存储过程:

USE [c1]
GO
/****** Object:  StoredProcedure [dbo].[Campaign_InsertRewardsVoucher]    Script Date: 10/22/2011 23:33:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
*/
ALTER PROCEDURE [dbo].[Campaign_InsertRewardsVoucher]  

    @voucherXML         XML, -- @voucherXML is the XML pass from my SQL server 

AS  
BEGIN

declare @changedVal int
set @changedVal = 4 

UPDATE  [Customers] 
SET  voucherXML.modify('replace value of (/CampaignVoucher/Qty/text())[1] with "0"') 
WHERE  voucherXML.value('(/CampaignVoucher/Qty)[1]', 'int') = @changedVal 


END
4

2 回答 2

2

您忽略了根级别节点<ArrayOfCampaignVoucher>- 试试这个:

UPDATE 
   dbo.[Customers] 
SET 
   voucherXML.modify('replace value of (/ArrayOfCampaignVoucher/CampaignVoucher/Qty/text())[1] with "0"') 
WHERE  
   voucherXML.value('(/ArrayOfCampaignVoucher/CampaignVoucher/Qty)[1]', 'int') = @changedVal 
于 2011-10-22T17:53:06.580 回答
0

或者,您可以执行以下操作,假设 SQL Server 支持后代或自我轴,并假设 Qty 没有其他祖先:

UPDATE 
  dbo.[Customers] 
SET 
  voucherXML.modify('replace value of (//Qty/text())[1] with "0"') 
WHERE  
  voucherXML.value('(//Qty)[1]', 'int') = @changedVal 

但似乎每个数组有多个 CampaignVoucher 节点,并且您不希望用新值批量替换值的第一个实例。相反,您不想将更新限制为具有特定 VouCode 的凭证吗?

于 2011-10-23T16:13:35.870 回答