MySQL存储过程语法中的变量错误

时间:2015-10-29 08:26:38

标签: mysql stored-procedures procedure

我有表组,其中包括课程计数和工作日列。我想编写MySQL程序,将数据添加到另一个名为课程的表中。但我无法处理MySQL的语法。你能帮我解决一下这个程序的问题:

CREATE PROCEDURE simpleproc (IN idGroup INT, IN groupName varchar(20),IN beginDate date, IN weekday1 INT, IN weekday2 INT, IN lessonCount INT)
BEGIN
DECLARE  i;
SET i:=1;
WHILE i<=lessonCount  DO
    DATE_ADD(beginDate,INTERVAL 1 DAY)
IF (WEEKDAY(beginDate)=weekday1) OR (WEEKDAY(beginDate)=weekday2) THEN
SET name:=groupName+i; 
SET price:=DIV(price,8)
   insert into lessons (lessonName, idGroup, lessonPrice, datePassed) 
   values (name,idGroup,price,begindate);
   SET i:=i+1
   END IF;



  END WHILE;


END

解决问题后,我会将此代码添加到Java中的预处理语句

1 个答案:

答案 0 :(得分:0)

这将运行。确保:1。在i循环内的代码的适当位置增加while。 2.避免使用字段和变量的保留字(如name)。 3.在对其进行整数除法之前,在某处定义price变量。您知道代码和表的结构。没有人能够为你做到这一点。

DROP PROCEDURE IF EXISTS `simpleproc`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `simpleproc`(IN idGroup INT, IN groupName varchar(20),IN beginDate date, IN weekday1 INT, IN weekday2 INT, IN lessonCount INT)
BEGIN
DECLARE i int;
DECLARE name1 int;
DECLARE price int;

SET  i:=1;
WHILE i<=lessonCount  DO
  SET beginDate:=DATE_ADD(beginDate,INTERVAL 1 DAY);
  IF WEEKDAY(beginDate) in(weekday1,weekday2) THEN
    SET name1:=groupName+i; 
    SET price:=price DIV 8;
    insert into lessons (lessonName, idGroup, lessonPrice, datePassed) 
      values (name1,idGroup,price,begindate);
  END IF;
  SET i:=i+1;
END WHILE;
END
;;
DELIMITER ;