意外的MYSQL语法错误

时间:2016-10-25 13:45:56

标签: mysql

我尝试使用下面的url中的代码创建SP,但是它显示了许多语法错误。 https://dev.mysql.com/doc/refman/5.7/en/cursors.html

CREATE PROCEDURE curdemo()
BEGIN
  DECLARE done INT DEFAULT FALSE;
  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 = TRUE;

  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;

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

首先更改分隔符

delimiter ||
CREATE PROCEDURE curdemo()
BEGIN
 ...
END
||
delimiter ;

否则,数据库引擎会认为您的过程定义在第一个;处结束,这会使其不完整。