关键字'case'附近的语法不正确

时间:2021-12-15 22:50:27

what is wrong with this Code

这个守则有什么问题

CREATE FUNCTION [dbo].[ChangeRevision] (@oldRev tinyint)
RETURNS varchar(1) 
AS
begin

declare @newRev varchar(1)
DECLARE @newval int
set @newval=CAST (@oldRev as int)

case @newval
begin
when 0 then set @newRev='Z'
when 1 then set @newRev='A'
when 2 then set @newRev='B'
when 3 then set @newRev='C'

end
return @newRev;

END

i have following error Incorrect syntax near the keyword 'case'.

我有以下错误关键字'case'附近的语法不正确。

Incorrect syntax near the keyword 'Return'.

关键字“返回”附近的语法不正确。

3 个解决方案

#1


10  

This should work:

这应该工作:

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)

#2


1  

case doesn't need a begin, but does need an end

case不需要开头,但确实需要结束

e.g.

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)

MSDN CASE Docs

MSDN CASE文档

#3


1  

there is not BEGIN keyword for case in tsql

在tsql中没有BEGIN关键字

select @newRev=case @newval
when 0 then 'Z'
when 1 then 'A'
when 2 then 'B'
when 3 then 'C'
end

#1


10  

This should work:

这应该工作:

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)

#2


1  

case doesn't need a begin, but does need an end

case不需要开头,但确实需要结束

e.g.

SET @newRev = (SELECT case @newval
    WHEN 0 THEN 'Z'
    WHEN 1 THEN 'A'
    WHEN 2 THEN 'B'
    WHEN 3 THEN 'C'
    END)

MSDN CASE Docs

MSDN CASE文档

#3


1  

there is not BEGIN keyword for case in tsql

在tsql中没有BEGIN关键字

select @newRev=case @newval
when 0 then 'Z'
when 1 then 'A'
when 2 then 'B'
when 3 then 'C'
end