0

I tried to create the SP using the code from the url below , however it display many syntax error. 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

6
  • INT DEFAULT FALSE; this is the reason for syntax error change this to INT DEFAULT 0; Commented Oct 25, 2016 at 13:47
  • Change the delimiter before your procedure. For instance like this: delimiter || Commented Oct 25, 2016 at 13:48
  • @ ocks, please see my second pic. Commented Oct 25, 2016 at 13:49
  • @ juergen d, sorry I cannot understand , how to Change the delimiter before your procedure? Commented Oct 25, 2016 at 13:50
  • @juergen d , can you give me the details steps? Commented Oct 25, 2016 at 13:51

1 Answer 1

1

Change your delimiter first

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

Otherwise the DB engine thinks your procedure definitions ends at the first ; which would make it incomplete.

Sign up to request clarification or add additional context in comments.

1 Comment

great , successfully after adding the delimiter ||.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.