MySql Cursor - 创建过程 [英] MySql Cursor - Creating a procedure
问题描述
我第一次尝试创建光标。我看了文档,我理解的概念,但我似乎不能得到它甚至被宣布...
我使用:
- MySql 5.1.41
- SqlYog作为经理
- (在xampp安装程序上本地运行)
即使复制粘贴 http://dev.mysql.com/doc/refman/5.1/en/cursors.html
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop:LOOP
FETCH cur1 INTO a,b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
如果b < c THEN
INSERT INTO test.t3 VALUES(a,b);
ELSE
INSERT INTO test.t3 VALUES(a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
我立即收到错误:
错误代码:1064
您的SQL语法有错误;
检查对应于
的手册您的MySQL服务器版本
右键语法在第3行附近使用
和其他一些人,
这对我没有任何意义,任何种类的灵魂可以帮助我吗?
谢谢
因此我得到了示例查询工作(感谢ajreal),重置了DELIMITER。但是当我运行我的查询:
DELIMITER ##
CREATE PROCEDURE RetiraPoints()
BEGIN
DECLARE userid BIGINT;
DECLARE done INT DEFAULT 0;
DECLARE cur CURSOR FOR SELECT uid FROM viewpoints;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop:LOOP
FETCH cur INTO userid;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO points(iduser,points,pointcat)VALUES(uid,-1,1),(userid,-1,2),(userid,-1,3),(userid,-1,4) ,(userid,-1,5),(userid,-1,6);
END LOOP;
CLOSE cur;
END; ##
我得到:
错误代码:1064
您的SQL语法有错误;
检查对应于
的手册您的MySQL服务器版本的
右键语法使用接近'DECLARE done
INT DEFAULT 0; DECLARE cur CURSOR FOR
SELECT uid FROM viewpoints; 'at line
1
我的上帝,这很难...
delimiter ##
...
end ##
需要在分隔符后面加一个空格
结尾 END
不需要;
i'm trying to create a cursor for the first time. I have looked at the documentation, i understand the concept, but i can't seem to get it to even be declared...
I'm using:
- MySql 5.1.41
- SqlYog as a manager
- (running locally on a xampp instalation)
Even when copy pasting the example found in http://dev.mysql.com/doc/refman/5.1/en/cursors.html
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
I get errors right away: Error Code : 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
and a bunch of others following,
this doesn't make any sense to me, can any kind soul help me please?
Thank you
So i got the sample query to work (thanks to ajreal), with resetting the DELIMITER. But when i run my query:
DELIMITER##
CREATE PROCEDURE RetiraPoints()
BEGIN
DECLARE userid BIGINT;
DECLARE done INT DEFAULT 0;
DECLARE cur CURSOR FOR SELECT uid FROM viewpoints;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
FETCH cur INTO userid;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO points (iduser, points, pointcat) VALUES (uid, -1, 1), (userid, -1, 2), (userid, -1, 3), (userid, -1, 4), (userid, -1, 5), (userid, -1, 6);
END LOOP;
CLOSE cur;
END;##
i get: Error Code : 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 'DECLARE done INT DEFAULT 0; DECLARE cur CURSOR FOR SELECT uid FROM viewpoints; ' at line 1
my god, this is hard...
You forget to reset the delimiter to NOT ;
delimiter ##
...
end##
need to put a space right after delimiter
And the ending END
does not require ;
这篇关于MySql Cursor - 创建过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!