[英]MySQL stored procedure syntax error in variables
I have table group with begindate lesson count and weekdays columns. 我有带有begindate课数和工作日列的表组。 I would like write MySQL procedure for adding data to another table named lessons according to group.
我想编写MySQL过程,以便根据组将数据添加到名为教训的另一个表中。 But I couldn't handle with syntax of MySQL.
但是我无法处理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 解决问题后,我将此代码添加到Java中准备好的语句中
This will run. 这将运行。 Make sure: 1. That you increment
i
in a proper place of your code inside the while
loop. 确保:1.在
while
循环内的代码的适当位置增加i
。 2. Avoid usind reserved words (like name
) for fields and variables. 2.避免为字段和变量使用usind保留字(如
name
)。 3. Define price
variable somewhere before making integer division of it. 3.在进行整数除法之前,在某处定义
price
变量。 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 ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.