我在下面的项目中遇到错误,我不明白为什么。任何人都可以为我解释一下吗?
CREATE FUNCTION dbo.fn_AcMonthOrder
(
@Month varchar(100)
)
RETURNS INT
AS
BEGIN
DECLARE @MonthOrder Int
SET @MonthOrder =
(CASE
WHEN @Month IN ('Aug','August',8) THEN 1
WHEN @Month IN ('Sep','September',9) THEN 2
WHEN@Month IN ('Oct','October',10) THEN 3
...
ELSE 0 END)
RETURN @MonthOrder
END
如果我尝试调用此函数,那么它对整数有效,但不适用于 varchars。即 PRINT @dbo.fn_AcMonthOrder(8) 将按预期返回 1,但 PRINT @dbo.fn_AcMonthOrder('Aug') 或 PRINT @dbo.fn_AcMonthOrder('August') 返回以下错误:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Aug' to data type int.