我想用光标创建一个事件。但是得到一个错误
错误:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
代码:
create event api_5 ON SCHEDULE EVERY 360 SECOND DO
BEGIN
DECLARE lc_current_time DATETIME;
DECLARE unuse_count INT;
DECLARE auto_assign TINYINT;
DECLARE total_sum INT;
DECLARE check_count INT;
SET lc_current_time = CONVERT_TZ(NOW(), @@session.time_zone, '+0:00');
DECLARE select_cursor CURSOR FOR SELECT unuse_count,auto_assign,total_sum,check_count from new_api;
OPEN select_cursor;
LOOP
FETCH select_cursor INTO unuse_count,auto_assign,total_sum,check_count
if(unuse_count <= check_count){
insert into api_report(date,new_api_id,api_name,type,status,email,customer_id,unuse_count,check_count) values (lc_current_time,5,'abc',4,1,'abc@gmail.com',check_count,unuse_count);
}
END;
END LOOP;
CLOSE select_cursor;
END;
任何人都想建议为什么它会在第三行显示出来。有一些限制,比如我们不能多次变量。 我是第一次尝试这个。所以请忽略任何错误。
注意:插入查询和选择查询都工作正常。
第二个错误:
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE select_cursor CURSOR FOR SELECT unuse_count,auto_assign,total_sum,check_' at line 10
答案 0 :(得分:0)
如果您还没有使用分隔符,请尝试这可以帮助您:
delimiter $$
create event api_5 ON SCHEDULE EVERY 360 SECOND DO
BEGIN
DECLARE lc_current_time DATETIME;
DECLARE unuse_count INT;
DECLARE auto_assign TINYINT;
DECLARE total_sum INT;
DECLARE check_count INT;
SET lc_current_time = CONVERT_TZ(NOW(), @@session.time_zone, '+0:00');
DECLARE select_cursor CURSOR FOR SELECT unuse_count,auto_assign,total_sum,check_count from new_api;
OPEN select_cursor;
LOOP
FETCH select_cursor INTO unuse_count,auto_assign,total_sum,check_count
if(unuse_count <= check_count){
insert into api_report(date,new_api_id,api_name,type,status,email,customer_id,unuse_count,check_count) values (lc_current_time,5,'abc',4,1,'abc@gmail.com',check_count,unuse_count);
}
END;
END LOOP;
CLOSE select_cursor;
END
delimiter ;
答案 1 :(得分:0)
以下是答案:
修正:
未添加分隔符。这给出了第一个错误,所以在开始和结束时添加了分隔符。
如果没有完成,所有变量声明都已开始使用它。
在回答中,我将光标放在SET lc_current_time = CONVERT_TZ(NOW(), @@session.time_zone, '+0:00');
之后。
If condition
未根据mysql语法添加
工作解决方案:
delimiter $$
create event api_6 ON SCHEDULE EVERY 360 SECOND DO
BEGIN
DECLARE lc_current_time DATETIME;
DECLARE unuse_count INT;
DECLARE auto_assign TINYINT;
DECLARE total_sum INT;
DECLARE check_count INT;
DECLARE select_cursor CURSOR FOR SELECT unuse_count,auto_assign,total_sum,check_count from new_api;
SET lc_current_time = CONVERT_TZ(NOW(), @@session.time_zone, '+0:00');
OPEN select_cursor;
LOOP
FETCH select_cursor INTO unuse_count,auto_assign,total_sum,check_count;
IF unuse_count <= check_count THEN
insert into api_report(date,new_api_id,api_name,type,status,email,customer_id,unuse_count,check_count) values (lc_current_time,5,'abc',4,1,'abc@gmail.com',check_count,unuse_count);
END IF;
END LOOP;
CLOSE select_cursor;
END$$
delimiter ;