Unknown Error When Comparing Values In Stored Procedure

  • Hi All,

    I have a stored procedure, which I'm calling from ASP.Net application. I'm facing a problem in stored procedure when comparing values.

    Please check code below to get further details.

    Stored Procedure:

    =========================================

    ALTER PROCEDURE [dbo].[MyStoredProcedure]

    (@ItemID nvarchar(50), @Weight1 float, @Weight2 float)

    -- check remaining/available weight in items table

    DECLARE @RemainingWeight float

    SET @RemainingWeight = (SELECT RemainingWeight FROM items WHERE ItemID = @ItemID)

    -- if supplied (@Weight1 + @Weight2) > @RemainingWeight , throw error

    IF (@Weight1 + @Weight2) > @RemainingWeight

    BEGIN

    DECLARE @Error nvarchar(2000)

    SET @Error = 'Weight1: ' + CAST(@Weight1 AS nvarchar(50)) + ', Weight2: ' + CAST(@Weight2 AS nvarchar(50)) + ', Remaining/Available Weight: ' + CAST(@RemainingWeight AS nvarchar(50)) + ', Total Supplied Weight: ' + CAST((@Weight1 + @Weight2) AS nvarchar(50))

    RAISERROR(@Error, 16, 1)

    Return -1

    END

    ..................................

    ..................................

    =========================================

    Supplied Values Through ASP.Net Page:

    =========================================

    Weight1 = 0.67

    Weight2 = 0

    Data type is double i.e. DbType.Double

    =========================================

    When I run the application, It goes into IF block (stored procedure) even if (@Weight1 + @Weight2) <= @RemainingWeight, and raise error.

    Error:

    =========================================

    Error Message: Weight1: 0.67, Weight2: 0, Remaining/Available Weight: 0.67

    =========================================

    The IF condition should not be true in this scenario because supllied values is equal to reamining/available weight, i.e.

    Weight1 + Weight2 = (0.67 + 0 ) = 0.67

    Remaining/Available Weight = 0.67

    I don't know what and where is the problem 🙁

    Please help me out if you have any idea.

    Thank you so much for your time and interest 🙂

    ------------

    Adnan

  • I think it has to do with floats. Why are you using floats? If you change them to decimal(10,2), does it work?

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Let me check this out.

    Thank you so much for your help 🙂

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

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