无法使简单的MySQL REPEAT工作

时间:2015-04-17 15:23:22

标签: mysql

请告诉我这有什么问题。它完全遵循5.5手册中的语法:

SET @RunID = 55;
REPEAT 
SET @RunID = @RunID + 1;
UNTIL @RunID = 100 
END REPEAT;

它一直告诉我REPEAT中有语法错误。 我的猜测是,它可能与MySQL在Mac OS X上运行并且我使用的客户端是Windows XP的事实有关。行尾字符?

2 个答案:

答案 0 :(得分:0)

UNTIL的问题。我想你想运行Loop UNTIL RunID已经达到100,而不仅仅是RunID IS 100 ...

试试这个:

delimiter //
CREATE PROCEDURE dorepeat2(p1 INT)
BEGIN
  SET @RunID = p1;
  REPEAT
      SET @RunID = @RunID + 1;
    SELECT @RunID;
  UNTIL @RunID <= 100 END REPEAT;
END
//
CALL dorepeat2(55)//

答案 1 :(得分:0)

看起来它比我想象的要复杂一点。这有效:

USE pim;           # trying to run it complained no database was in use
SET @RunID = 55;   # can't define a user variable inside a procedure(?)
DELIMITER //       # so the procedure code can be parsed as 1 statement
CREATE PROCEDURE dorepeat()
BEGIN
    REPEAT 
      SET @RunID = @RunID + 1;
      UNTIL @RunID = 100 
    END REPEAT;
END 
//
DELIMITER ;        # restore the standard MySQL delimiter

CALL dorepeat();   # call the procedure
SELECT @RunID;     # returns 100