如何在phpmyadmin中声明游标?

xxslljrj  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(403)

当我添加这个代码时,我有一个错误,我无法修复它。

DECLARE c1 cursor FOR SELECT i FROM integers;
OPEN c1;
FETCH c1 INTO L1;
FETCH c1 INTO L2;
FETCH c1 INTO L3;
FETCH c1 INTO L4;
FETCH c1 INTO L5;
FETCH c1 INTO L6;
CLOSE c1;

下面是完整的代码。在没有上面的代码的情况下,过程执行并返回integer表中的六行没有重复的数字,我想将其写入l1、l2、l3、l4、l5、l6,然后插入表kupony中过程参数中有“多少”。

DELIMITER $$

CREATE PROCEDURE addTickets (IN howMany INT)
BEGIN

DECLARE L1 INT default 0;
DECLARE L2 INT default 0;
DECLARE L3 INT default 0;
DECLARE L4 INT default 0;
DECLARE L5 INT default 0;
DECLARE L6 INT default 0;
DECLARE count1 INT;
set count1 = 0;

while count1 < howMany DO
DECLARE c1 FOR SELECT i FROM integers WHERE i BETWEEN 1 AND 49 ORDER BY RAND() LIMIT 6;
OPEN c1;
FETCH c1 INTO L1;
FETCH c1 INTO L2;
FETCH c1 INTO L3;
FETCH c1 INTO L4;
FETCH c1 INTO L5;
FETCH c1 INTO L6;
CLOSE c1;    
INSERT INTO kupony VALUES(NULL, 1, L1, L2, L3, L4, L5, L6, -1, '2018-01-01', -1);
set count1 = count1 + 1;
end while;

END$$

DELIMITER ;
v1uwarro

v1uwarro1#

你必须放一个 BEGIN ... END; 围绕嵌套光标的块。

DELIMITER $$

CREATE PROCEDURE addTickets (IN howMany INT)
BEGIN

DECLARE L1 INT default 0;
DECLARE L2 INT default 0;
DECLARE L3 INT default 0;
DECLARE L4 INT default 0;
DECLARE L5 INT default 0;
DECLARE L6 INT default 0;
DECLARE count1 INT default 0;

while count1 < howMany DO
BEGIN
DECLARE c1 CURSOR FOR SELECT i FROM integers WHERE i BETWEEN 1 AND 49 ORDER BY RAND() LIMIT 6;
OPEN c1;
FETCH c1 INTO L1;
FETCH c1 INTO L2;
FETCH c1 INTO L3;
FETCH c1 INTO L4;
FETCH c1 INTO L5;
FETCH c1 INTO L6;
CLOSE c1;    
INSERT INTO kupony VALUES(NULL, 1, L1, L2, L3, L4, L5, L6, -1, '2018-01-01', -1);
set count1 = count1 + 1;
END;
end while;

END$$

DELIMITER ;

相关问题