sql server 2012:关键字'else'附近的语法不正确
问题描述:
我收到此错误,我找不到原因。sql server 2012:关键字'else'附近的语法不正确
case when FormFields.fieldtype like '%date%'
then 'not' + fieldname + ' is null and (convert(datetime,'
+ fieldname +',103) < '
+ coalesce(str(FormFields.MinDate),'1/1/1900') + ')'
+ ' or (convert(datetime,' + fieldname +',103) > '
+ coalesce(str(FormFields.MaxDate), '1/1/2200') + ')'
+
else
'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <'
+ coalesce(str(FormFields.MinValue),'-99999999')
+ ' or convert(float,'+ fieldname+') >'
+ coalesce(str(FormFields.MaxValue),'99999999') +')'
+
end
前面的代码没有任何错误:
'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <'
+ coalesce(str(FormFields.MinValue),'-99999999')
+ ' or convert(float,'+ fieldname+') >'
+ coalesce(str(FormFields.MaxValue),'99999999') +')'
+
我只是想补充另一种情况
答
您的查询2个额外的+
,1在THEN
部分的结束,另一个在ELSE
部分的末尾。如果您需要将CASE
表达式与另一个字符串结合使用,请在END
之后使用+
。请尝试以下方法:
case when FormFields.fieldtype like '%date%'
then 'not' + fieldname + ' is null and (convert(datetime,'
+ fieldname +',103) < '
+ coalesce(str(FormFields.MinDate),'1/1/1900') + ')'
+ ' or (convert(datetime,' + fieldname +',103) > '
+ coalesce(str(FormFields.MaxDate), '1/1/2200') + ')'
else
'not '+ fieldname + ' is null and (convert(float,'+ fieldname+') <'
+ coalesce(str(FormFields.MinValue),'-99999999')
+ ' or convert(float,'+ fieldname+') >'
+ coalesce(str(FormFields.MaxValue),'99999999') +')'
end
+ 'Any string after CASE expression'
也许在'then'行结尾的超级+符号? –
结尾 –
还有另外一个建筑字符串代码的一部分,这就是为什么有额外的'+' – aggicd