T-SQL How to end an IF-ELSE IF-ELSE block

前端 未结 6 1649
野的像风
野的像风 2021-02-12 10:44

When I run the below procedure with the correct parameters so that the -1 value isn\'t returned, none of my DML statements are firing. I\'m guessing that it\'s treating all my D

6条回答
  •  北海茫月
    2021-02-12 11:08

    In your case (pun intended), you might be better off using a CASE WHEN construction, seeing you want to evaluate different values for your @Code variable. MSDN states that CASE is intended exactly for such scenarios:

    Evaluates a list of conditions and returns one of multiple possible result expressions.
    

    I find it makes code a bit more readable for simple evaluations (but that could very well be a personal preference).

    Your code would end up looking similar to this (pseudo-code. not tested):

    CASE @Code 
        WHEN 'KP' THEN SET @stringConcat += 'Y';
        WHEN 'RL' THEN SET @stringConcat += 'Z';
        ElSE
            -- Return error code and stop processing
            SELECT -1;
            RETURN;
    END 
    

    More on the CASE statement here:

    https://msdn.microsoft.com/en-us/library/ms181765.aspx

提交回复
热议问题