我有一张像这样的桌子:
编号
数字=整数,编码=整数
SELECT * FROM number WHERE number = 21377 and cod = 55;
返回正确的值;
所以我有一个proc调用来插入:
CREATE DEFINER=`root`@`%` PROCEDURE `create`(IN Numb INT, IN Cod INT, IN qt INT)
BEGIN
SET @Cont = 0;
SET @Init = Numb;
WHILE @Cont < qt DO
SET @Exist = (SELECT count(number) FROM rnumber WHERE number = @Init AND cod = Cod LIMIT 1);
IF @Exist = 0 THEN
INSERT INTO (...)
END IF;
SET @Cont = @Cont + 1;
SET @IniT = @Init + 1;
END WHILE;
END$$
拨打
create
(21377,54,1);始终给我变量@Exist为1,因此即使数字和鳕鱼的组合不存在,也不要继续进行。
谁能指出我做错了什么?
谢谢。
最佳答案
尝试使用select into代替set xx =(select ...)。
还有cod = Cod
总是会返回true ...
您还需要声明变量...
试试这个:
CREATE DEFINER=`root`@`%` PROCEDURE `create`(IN p_Numb INT, IN p_Cod INT, IN p_qt INT)
BEGIN
declare v_Exist integer;
declare v_Init integer;
declare v_Exist integer;
SET v_Cont = 0;
SET v_Exist = 0;
SET v_Init = p_Numb;
WHILE v_Cont < p_qt DO
SELECT count(number) into v_Exist FROM rnumber WHERE number = v_Init AND cod = p_Cod LIMIT 1;
IF v_Exist = 0 THEN
INSERT INTO (...)
END IF;
SET v_Cont = v_Cont +1;
SET v_IniT = v_Init + 1;
END WHILE;
END$$