0

I have created user defined function in SQL Server, but somehow the logic is not correct, it always executes the else statement; please help!

CREATE FUNCTION shipSelection
    (@Item_No varchar(20),@itemname varchar(20))
RETURNS varchar(100)
AS
BEGIN
    declare @weight float
    declare @demention float
    declare @density float
    declare @result varchar(100)

    SET @weight = (SELECT Item_Weight FROM Items 
                   WHERE Item_No = @Item_No AND Name = @itemname) 
    SET @demention = (SELECT Dimention FROM Items 
                      WHERE Item_No = @Item_No AND Name = @itemname)

    SELECT @density = @weight / @demention

    IF @density > 100
    BEGIN
        SET @result = 'LOW DENSITY CAN USE ANY TYPE SHIPS';
    END
    ELSE IF @density > 300
    BEGIN
       SET @result = 'MEDIUM DENSITY CAN USE MEDIUM SHIPS';
    END
    ELSE IF @density > 500
    BEGIN
        SET @result = 'MEDIUM DENSITY WANT TO USE HUGE SHIPS';
    END
    ELSE 
    BEGIN
       SET @result = 'DONT WANT TO CARE ABOUT SHIPS TOO SMALL DENSITY ITEM';
    END

    RETURN @result
END

SELECT 
    dbo.shipSelection('I010', 'asus x555l') AS TRANSPORT_SHIP_TYPE
3
  • Can you show the record that you are looking up please Commented Jul 23, 2016 at 19:42
  • I suggest that you add some debug code to select the whole record of to show the result when you populate dimension and weight Commented Jul 23, 2016 at 19:44
  • That's, how I pass values to function Commented Jul 23, 2016 at 19:58

2 Answers 2

4

The way you have the logic, it's always going to hit either the first case (> 100), or the else (< 100). Try checking the largest numbers first.

It will never hit the > 300 case, because it will use the > 100 case first.

If you are always hitting the else statement, you'll need to check that your value is actually greater than 100. Sounds like it isn't.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for getting back to us that it fixed your problem. It is customary to accept the correct answer ( @PhillipXT) so that he gets the credit and to help future users with a similar problem to find the correct answer.
0

I bet that one of your queries returns a NULL value. Since null is not a number and cannot be compared with a number your else statement, in the case of null, will be hit. You can prevent this by setting the value of the query returning null to a known value using COALESCE.

    SET @weight = (SELECT COALESCE(Item_Weight,0) FROM Items 
                   WHERE Item_No = @Item_No AND Name = @itemname) 
    SET @demention = (SELECT COALESCE(Dimention,0) FROM Items 
                      WHERE Item_No = @Item_No AND Name = @itemname)

4 Comments

The demention variable should maybe default to 1, because he's doing division with it afterwards.
Personally, I prefer checking for divide by zero condition with a CASE statement rather than setting the value using COALESCE. If @demention is used after the division then you will need to keep a mental tally of it being 1 instead of zero. Just my two cents.
SELECT @a=CASE WHEN @c=0 THEN 0 ELSE @b/ @c END...however you are not wrong and I am not right:)
As long as it's checked for before the division, it's all good :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.