How to use a case statement in scalar valued function in SQL?

前端 未结 6 1449
半阙折子戏
半阙折子戏 2020-12-31 04:26

I want to get one value from a function using a case statement. I tried the following but it does not work:

CREATE FUNCTION [FATMS].[fnReturnByPeriod]
(

            


        
6条回答
  •  情话喂你
    2020-12-31 05:05

    In what way does it "not work"? Does it produce an error when you try to execute it? Does it return an unexpected result?

    Off the top of my head, there are several problems:

    Your case statement returns 1 in all cases.

    The case statement is also combining two different types of CASE semantics:

    CASE @Period 
    WHEN  THEN 
    WHEN  THEN 
    ELSE 
    END
    

    or

    CASE
    WHEN @Period =  THEN 
    WHEN @Period =  THEN 
    ELSE 
    END
    

    The second form allows you to use unrelated conditions, whereas the first can only check for different values of @Period.

    Further, you're returning the value of @Period, not the value generated by the CASE statement.

提交回复
热议问题