声明 mariaDB 错误。如何修复此查询?

Declare mariaDB error. How can fix this query?

我正在尝试用另一个 table 的一些列填充一个 table 并给出一个 id,但我收到了这个错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'type of products.CategoryID; DECLARE t_productid type of products.ProductID;' at line 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 ;

您可以使用显式类型,或升级到引入了 TYPE OF 语法的 mariadb-10.3。

此解决方案不需要这些语句,从而避免了语法错误!

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;