2013-03-25 82 views
0

我創建一個過程,找出從表中缺少ID,但它並沒有取得任何結果,以下是過程MySQL檢索表的缺失自動增量ID的過程?

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; || 

然後我致電上述

CALL rpt_proc(); 

但沒有結果在tbl_missing,而我可以看到有film_idtbl_film有很多缺失的值。有人會告訴我我做錯了什麼嗎?

回答

0

試試這段代碼。

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; ||