我想创建一个处理来自我们网站的订单的存储过程。问题是当我运行脚本时出现MySQL语法错误。我对MySQL存储过程非常新。有人可以查看我的代码吗?
USE postbrood;
DELIMITER //
CREATE PROCEDURE MaakBestelling(IN KlantIDParam INT, IN ProductIDArray VARCHAR(255), IN AantalArray VARCHAR(255))
BEGIN
DECLARE BestelID INT DEFAULT 0;
DECLARE ArrayLenght INT DEFAULT 0;
DECLARE Counter INT DEFAULT 0;
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
INSERT INTO bestelling(klantID) VALUES (KlantIDParam);
SET BestelID = LAST_INSERT_ID();
WHILE Counter < ArrayLenght DO
INSERT INTO bestelregel VALUES (SUBSTRING_INDEX(ProductIDArray,',',Counter),BestelID,SUBSTRING_INDEX(AantalArray,',',Counter));
SET Counter = Counter + 1;
END WHILE;
END//
DELIMITER ;
提前致谢!
答案 0 :(得分:1)
钉了它! :)
USE postbrood;
DELIMITER //
CREATE PROCEDURE MaakBestelling(IN KlantIDParam INT, IN ProductIDArray VARCHAR(255), IN AantalArray VARCHAR(255))
BEGIN
DECLARE BestelID INT DEFAULT 0;
DECLARE ArrayLenght INT DEFAULT 0;
DECLARE Counter INT DEFAULT 0;
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
INSERT INTO bestelling(klantID) VALUES (KlantIDParam);
SET BestelID = LAST_INSERT_ID();
WHILE Counter < ArrayLenght DO
INSERT INTO bestelregel VALUES (SUBSTRING_INDEX(ProductIDArray,',',Counter),BestelID,SUBSTRING_INDEX(AantalArray,',',Counter),2.50);
SET Counter = Counter + 1;
END WHILE;
END//
DELIMITER ;
答案 1 :(得分:0)
我认为逗号未被识别
SET ArrayLenght = LENGTH(ProductIDArray) - LENGTH(REPLACE(ProductIDArray, ',', '')) + 1;
您应该尝试使用ascii实体编号替换逗号:,