0

I create a procedure to find out missing id from a table, But it does not retrieve any result, below are the procedure

DELIMITER ||
DROP PROCEDURE IF EXISTS proc_rpt ||
CREATE PROCEDURE proc_rpt()
BEGIN 
SET @minID = (SELECT MIN(`id`) FROM `tbl_film` WHERE `user_id`=13);
SET @maxID = (SELECT MAX(`id`) FROM `tbl_film` WHERE `user_id`=13);
REPEAT 
    SET @tableID = (SELECT `id` FROM `tbl_film` WHERE `id` = @minID);
    IF (@tableID IS NULL) THEN
        INSERT INTO temp_missing(`missing_id`) VALUES (@tableID);
    END IF;
    SET @minID = @minID + 1; 
UNTIL (@minID <= @maxID)
END REPEAT;  // earlier I was missing this line which was giving error in Procedure.
END; ||

Then i call above

CALL rpt_proc();

But there are no result on tbl_missing while i can see there are many missing values of film_id in tbl_film. Will someone please tell me what I am doing wrong?

1 Answer 1

0

Have a try with this code.

DELIMITER ||
    DROP PROCEDURE IF EXISTS proc_rpt ||
    CREATE PROCEDURE proc_rpt()
    BEGIN 
    DECLARE maxId int;
    DECLARE minId int;
    DECLARE curId int;
        SELECT MIN(`id`) INTO maxId FROM `tbl_film` WHERE `user_id`=13;
        SELECT MAX(`id`) INTO minId FROM  `tbl_film` WHERE `user_id`=13;
    REPEAT 
        SELECT `id` INTO curId FROM `tbl_film` WHERE `id` = @minID);
        IF (curId IS NULL) THEN
            INSERT INTO temp_missing(`missing_id`) VALUES (curId);
        END IF;
        SET minId = minId + 1; 
    UNTIL (minId <= maxId)
    END REPEAT;  // earlier I was missing this line which was giving error in Procedure.
    END; ||
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.