SQL替换和CAST(PromotionRuleData为NVARCHAR(MAX))

时间:2013-12-02 17:10:59

标签: sql sql-server

似乎无法编辑我的旧帖子,但我正在尝试执行此SQL脚本

UPDATE Promotions 
set Description = '£5 Off £25 Spend', 
UsageText = '£5 Off £25 Spend',
EmailText = '£5 Off £25 Spend',
PromotionRuleData= '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-13T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed>    </PromotionRuleBase><PromotionRuleBase xsi:type="MinimumCartAmountPromotionRule"><CartAmount>24.99</CartAmount></PromotionRuleBase></ArrayOfPromotionRuleBase>',
PromotionDiscountData = '<ArrayOfPromotionDiscountBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionDiscountBase xsi:type="OrderPromotionDiscount"><DiscountType>Fixed</DiscountType><DiscountAmount>5.00</DiscountAmount></PromotionDiscountBase></ArrayOfPromotionDiscountBase>'
where Name = 'test1,test2,etc...'

它出现了这个错误

  

Msg 402,Level 16,State 1,Line 1
  数据类型varchar和text在等于运算符中不兼容。

我尝试使用where CAST(PromotionRuleData as NVARCHAR(MAX))

因此该行显示为

CAST(PromotionRuleData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase ...

但没有运气。

1 个答案:

答案 0 :(得分:0)

您无法将字符串文字与SQL Server中的text列进行比较。

哪个列的数据类型为text?您在name子句中使用的WHERE列是否有机会?

如果是,请改用WHERE

WHERE CAST(Name AS VARCHAR(MAX)) = 'test1,test2,etc...'

或更好:将您的列转换为更合适的数据类型,例如VARCHAR(n)(除非您真的需要2 GB = 20亿个字符 - 如果是这样,那么请使用VARCHAR(MAX)