nnmmss
nnmmss

Reputation: 2974

Incorrect syntax near the keyword 'case'

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'.

Incorrect syntax near the keyword 'Return'.

Upvotes: 7

Views: 35938

Answers (3)

Prahalad Gaggar
Prahalad Gaggar

Reputation: 11599

there is not BEGIN keyword for case in tsql

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

Upvotes: 1

Caleth
Caleth

Reputation: 62636

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

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

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460068

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)

Upvotes: 15

Related Questions