0

The following stored procedure returns nothing even if the matching data exists in the database.

DELIMITER $$

    DROP PROCEDURE IF EXISTS `portaldb`.`hasPrivateCloud`$$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `hasPrivateCloud`(in_userId int)
    begin
        if in_userId is not null then
            select (case when ui.privateCloud is null
                    then false
                    else ui.privateCloud
                    end) as hasPrivateCloud from userinfo as ui where ui.userid = in_userId;
        end if;
    end$$

    DELIMITER ;

privateCloud is a boolean field. If the field is false or where clause is not satisfied, the procedure should return false and when where clause is satisfied, it should return the column value. But it always returns an empty column.

What could be the reason for it?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • Maybe this following stackoverflow post [display bit values in select](http://stackoverflow.com/questions/14248554/cant-see-mysql-bit-field-value-when-using-select) would help – g3suya Nov 17 '16 at 11:21

2 Answers2

1

When the record doesn't exist, nothing is returned. Not, like you assume, a NULL record.

You could write it like

IF EXISTS (SELECT privateCloud FROM userinfo WHERE userid = in_userId)
    SELECT privateCloud FROM userinfo WHERE userid = in_userId;
ELSE
    SELECT 'false';
END IF;

When you're so concerned about performance, you can even do

IF EXISTS (SELECT @result := privateCloud FROM userinfo WHERE userid = in_userId)
    SELECT @result;
ELSE
    SELECT 'false';
END IF;
fancyPants
  • 50,732
  • 33
  • 89
  • 96
0

Sorry about the earlier post. I deleted it. Thanks @fancyPants for pointing that out.

Thought i would add one more suggestion though

select (case when ifnull(ui.privateCloud,false)=false
                then 'False'
                else 'True'
                end) as hasPrivateCloud from userinfo as ui where ui.userid= in_userId;
g3suya
  • 19
  • 1
  • 4