ImpStudent
ImpStudent

Reputation: 123

MySql function not returning the correct result

I'm trying to create a function in mysql which will return boolean if the id already exist in the table. Most of the solutions that I found online require solutions using both php and mysql but what I'm trying to do is a solution purely in mysql in the form of a function.

My simplifed mysql table will be as follow:

CREATE TABLE IF NOT EXISTS table1 (
    `user_id` VARCHAR(12) UNIQUE,
    `name` VARCHAR(128),
    PRIMARY KEY (`user_id`)
);

My first attempt was below:

DELIMITER *
DROP FUNCTION IF EXISTS CheckExistId*
CREATE FUNCTION CheckExistId (user_id VARCHAR(12)) RETURNS BOOL
BEGIN
    DECLARE isExist BOOL;
    SET isExist = 0;
    SELECT EXISTS(SELECT * FROM table1 WHERE `user_id`=user_id) INTO isExist ;
    RETURN isExist;
END*
DELIMITER ;

My second attempt:

DELIMITER *
DROP FUNCTION IF EXISTS CheckExistId*
CREATE FUNCTION CheckExistId (user_id VARCHAR(12)) RETURNS BOOL
BEGIN
    DECLARE isExist BOOL;
    DECLARE countRow INT;
    SELECT COUNT(`user_id`) INTO countRow FROM table1 WHERE `user_id`=user_id;
    IF countRow = 0 THEN SET isExist = 1; 
    ELSE SET isExist = 0; 
    END IF;
    RETURN isExist;
END*
DELIMITER ;

Both didn't give me the result that I expected. Any suggestions on how to fix this?

Upvotes: 0

Views: 3731

Answers (3)

fancyPants
fancyPants

Reputation: 51888

Your first attempt was quite fine, except that you're using * as delimiter and using the same name for your parameter and column name. That's confusing MySQL. Write it like this:

DELIMITER $$
DROP FUNCTION IF EXISTS CheckExistId $$
CREATE FUNCTION CheckExistId (p_user_id VARCHAR(12)) RETURNS BOOL
BEGIN
    DECLARE isExist BOOL;
    SET isExist = 0;
    SELECT EXISTS(SELECT * FROM table1 WHERE `user_id`=p_user_id) INTO isExist ;
    RETURN isExist;
END $$
DELIMITER ;

Upvotes: 3

Anda Iancu
Anda Iancu

Reputation: 530

DROP TABLE IF EXISTS `TableTT2`;
CREATE TABLE IF NOT EXISTS TableTT2 (
    `user_id` VARCHAR(12) UNIQUE,
    `name` VARCHAR(128),
    PRIMARY KEY (`user_id`)
);

INSERT INTO `TableTT2` (`user_id`, `name`) VALUES
(LEFT(UUID(), 12), 'a1'), 
(LEFT(UUID(), 12), 'a2'), 
(LEFT(UUID(), 12), 'a3'), 
(LEFT(UUID(), 12), 'a4'), 
(LEFT(UUID(), 12), 'a5'), 
(LEFT(UUID(), 12), 'a6'), 
(LEFT(UUID(), 12), 'a7'), 
(LEFT(UUID(), 12), 'a8');


    DELIMITER $$

    CREATE FUNCTION `CheckExistId`(usrId varchar(12)) RETURNS BOOL
BEGIN

    DECLARE totalCnt INT;
    DECLARE resBool BOOL;
    SET totalCnt = 0 ;

    SELECT COUNT(*) INTO totalCnt FROM TableTT2 WHERE user_id = usrID; 
    SET resBool = IF(totalCnt <> 0, TRUE, FALSE);
    RETURN resBool;

END


SELECT * FROM TableTT2;
SELECT CheckExistId('fe3faa00-0b0');
SELECT CheckExistId('fe3faa00-111');   

Hope this helps. It might be an issue using * delimiter and a second issue that i noticed your function parameter is a varchar even if you check by an integer id.

Upvotes: 0

Kuzgun
Kuzgun

Reputation: 4737

DELIMITER *
DROP FUNCTION IF EXISTS CheckExistId*
CREATE FUNCTION CheckExistId (user_id VARCHAR(12)) RETURNS BOOL
BEGIN
SELECT 
    CASE 
        WHEN EXISTS(
             SELECT * FROM table1 WHERE `user_id`=user_id)
        THEN RETURN 1 

        ELSE RETURN 0 
    END
END*
DELIMITER ;

Upvotes: 0

Related Questions