-1

我有一个名为 runTotal 的字段名,它是一个整数。

我正在尝试将值分配给最高总数的 CASE 语句:

select (case when isnull(t.runtotal,90) = 0 then 'You have reached max total' else t.runtotal end) runtotal.

我收到以下错误:

Conversion failed when converting the varchar value 'Class is full' to data type int

如果我删除 Else t.runtotal,它可以工作,但我们希望在代码中使用 else,因为默认值应该是 90。

任何想法如何解决这一问题?

提前谢谢了

4

3 回答 3

3

那是因为您要返回不同的数据类型。
第一部分返回一个 varchar,而 else 部分返回一个数字。

select 
-- this condition returns a varchar
(case when isnull(t.runtotal,90) = 0 then 'You have reached max total' 
-- else condition is returning a number
else t.runtotal 
end) runtotal.

返回值的数据类型应该相同。
请改正。

于 2013-06-03T21:05:54.660 回答
0

这段代码在说

select (case when isnull(t.runtotal,90) = 0 
   then 'You have reached max total'
   else t.runtotal end)  runtotal

如果 runTotal = 0,则返回一个带有消息文本“您已达到最大总数”的字符串。否则,返回一个整数告诉我运行总数

这就是说,返回一个错误消息字符串,或者将运行总计的内容转换为一个字符串

select (case when isnull(t.runtotal,90) = 0 
   then 'You have reached max total'
   else Str(t.runtotal) end)  runtotal
于 2013-06-03T21:09:05.453 回答
0

我终于解决了这个问题。下面是最终解决方案:

(case when isnull(t.runtotal,9) = 0 then 'You have reached max total'
 else  str(isnull(t.runtotal,9)) end) runtotal
于 2013-06-04T12:36:01.903 回答