SQL Stored Procedure preventing to write null

后端 未结 3 1560
灰色年华
灰色年华 2021-01-29 00:08

So, I\'ve got this below implemented in one of my stored procedures. And if they have less points as specified it works correctly but if they have normal points (not less) and t

3条回答
  •  萌比男神i
    2021-01-29 00:43

    I'm still a little confused by your question, but this should work as I understand. Instead of all the IF statements, consider moving into a single CASE statement, defaulting the value to 0. Then add a check if the value isn't 0, to do something.

    DECLARE @Level int
    DECLARE @Total int
    DECLARE @NewPoint int
    
    SET @Level = 61
    SET @Total = 100
    
    SELECT 
      @NewPoint = CASE
                    WHEN @Level = 61 AND @Total < 482 THEN 10
                    WHEN @Level = 62 AND @Total < 487 THEN 15
                    WHEN @Level = 63 AND @Total < 492 THEN 20
                    WHEN @Level = 64 AND @Total < 497 THEN 25
                    WHEN @Level = 65 AND @Total < 502 THEN 30
                    ELSE 0
                  END
    
    IF @NewPoint <> 0 
    BEGIN
      SELECT 'DO SOMETHING'
    END
    
    • SQL Fiddle Demo

提交回复
热议问题