简体   繁体   中英

MySQL stored procedure syntax error in variables

I have table group with begindate lesson count and weekdays columns. I would like write MySQL procedure for adding data to another table named lessons according to group. But I couldn't handle with syntax of MySQL. Could you please help me resolve problems with that procedure:

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

After solving problems I will add this code to prepared statement in Java

This will run. Make sure: 1. That you increment i in a proper place of your code inside the while loop. 2. Avoid usind reserved words (like name ) for fields and variables. 3. Define price variable somewhere before making integer division of it. You know the code and your tables' structure. Nobody is capable to do it for you.

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM