gpt4 book ai didi

MySQL过程循环错误

转载 作者:行者123 更新时间:2023-11-29 06:44:40 24 4
gpt4 key购买 nike

当运行我的查询时,我得到了这个:

错误:

#1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use near '' at line 3

SQL:

DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP

IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();

最佳答案

你需要定义一个新的分隔符

delimiter |

DROP PROCEDURE IF EXISTS insertPromos |

CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP

IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|

delimiter ;

CALL insertPromos();

否则过程定义将在第一个 ; 结束,这是不正确的。

关于MySQL过程循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19292729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com