SQL更新查询无法将nvarchar转换为整数

时间:2017-09-11 09:08:07

标签: sql sql-server sql-update sql-convert

以下查询应返回由3或4位数组成的记录,这些记录从nvarchar转换为整数且低于1200.而不是所需的结果,错误返回说明:转换nvarchar值时转换失败&#39 ;'数据类型int。

UPDATE cvUpd
SET cvUpd.visible = case when CONVERT(int, cvFrom.ConfigText) < 1200 then 0 else 1 end
FROM ConfigValue cvUpd
INNER JOIN OptionRestriction r ON cvUpd.ConfigurationID=67061
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1 
   AND cvFrom.ConfigText=r.value
   AND cvFrom.ConfigText NOT LIKE '%[^0-9]%' 
   AND (LEN(cvFrom.ConfigText) = 3 or LEN(cvFrom.ConfigText) = 4)

我还尝试运行select查询,但此查询导致了同样的错误:

SELECT CONVERT(int, cvFrom.ConfigText)
FROM ConfigValue cvUpd
INNER JOIN OptionRestriction r ON cvUpd.ConfigurationID=67061
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1 
   AND cvFrom.ConfigText=r.value
   AND cvFrom.ConfigText NOT LIKE '%[^0-9]%' 
   AND (LEN(cvFrom.ConfigText) = 3 or LEN(cvFrom.ConfigText) = 4)

更新

即使不使用convert,我也会得到同样的错误:

SELECT cvFrom.ConfigText
FROM ConfigValue cvUpd
INNER JOIN OptionRestriction r ON cvUpd.ConfigurationID=67061
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1 
   AND cvFrom.ConfigText=r.value
   AND cvFrom.ConfigText NOT LIKE '%[^0-9]%' 
   AND (LEN(cvFrom.ConfigText) = 3 or LEN(cvFrom.ConfigText) = 4)

1 个答案:

答案 0 :(得分:0)

问题是r.value是一个与nvarchar“cvFrom.ConfigText”连接的整数。因此,我需要将AND cvFrom.ConfigText=r.value修改为AND cvFrom.ConfigText=CONVERT(nvarchar(max), r.value)