以下查询应返回包含从 nvarchar 转换为整数且小于 1200 的 3 或 4 位数字的记录。而不是所需的结果,而是返回错误声明:将 nvarchar 值 '' 转换为数据类型 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 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)
更新
即使不使用转换,我似乎也会遇到同样的错误:
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)