我试图用另一个提供ID的表中的某些列填充一个表,但出现此错误:
1064-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取在'type of products.CategoryID'中使用正确的语法。 DECLARE t_productid产品的类型。ProductID;'在第4行
我不知道如何解决它,希望您能为我提供帮助。
DELIMITER //
Create procedure getProducts(v_categoryid int)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE t_categoryid type of products.CategoryID;
DECLARE t_productid type of products.ProductID;
DECLARE t_productname type of products.ProductName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE cur1 CURSOR(p_categoryid int)
FOR
SELECT CategoryID, ProductID, ProductName
FROM products
WHERE CategoryID = p_categoryid;
create table IF NOT exists curProducts(
CategoryID int(10),
ProductID int(10),
ProductName varchar(40));
truncate table curProducts;
open cur1(v_categoryid);
read_loop: LOOP
FETCH cur1 INTO t_categoryid, t_productid, t_productname;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO curProducts VALUES (t_categoryid, t_productid, t_productname);
END LOOP;
close cur1;
select * from curProducts;
END //
DELIMITER ;
答案 0 :(得分:0)
您可以使用显式类型,也可以升级到引入了TYPE OF
语法的mariadb-10.3。
答案 1 :(得分:0)
此解决方案通过不需要这些语句来避免语法错误!
DECLARE done INT DEFAULT FALSE;
DECLARE t_categoryid type of products.CategoryID;
DECLARE t_productid type of products.ProductID;
DECLARE t_productname type of products.ProductName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE cur1 CURSOR(p_categoryid int)
FOR
SELECT CategoryID, ProductID, ProductName
FROM products
WHERE CategoryID = p_categoryid;
open cur1(v_categoryid);
read_loop: LOOP
FETCH cur1 INTO t_categoryid, t_productid, t_productname;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO curProducts VALUES (t_categoryid, t_productid, t_productname);
END LOOP;
close cur1;
所有这些都可以替换为
INSERT INTO curProducts VALUES (t_categoryid, t_productid, t_productname)
SELECT CategoryID, ProductID, ProductName
FROM products
WHERE CategoryID = p_categoryid;