Incorrect Result Returned From IF Statement

  • I have a stored procedure that receives two parameters of INT type and has two local variables also  of INT type. The two local variables are called @RetVal and @TempVal. What happens is the procedure queries one table and returns an integer value which is read into @RetVal, a second table is queried and returns an integer value which is read into @TempVal. What is supposed to happen next is that the values in @RetVal and @TempVal are compared, if @TempVal is less than @RetVal then @RetVal should be set to the value of @TempVal. The following is the syntax I have used:

    IF NOT (@TempVal) IS NULL

    IF @TempVal < @RetVal

    SET @RetVal = @TempVal

    PRINT @RetVal

    When the procedure is run it does not return the correct figure, if I comment out lines 1 and 2 it does return the correct figure.

  • Nice coding reduces misunderstandings and misreadings

    IF NOT (@TempVal) IS NULL

    Begin

       IF @TempVal < @RetVal

       begin

          SET @RetVal = @TempVal

         end

    end

    PRINT @RetVal

     

    or

    if isnull(@TempVal, @RetVal) < @RetVal     

    begin

      SET @RetVal = @TempVal

    end

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Thanks for that!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply