我正在尝试使用游标在存储过程中编写动态游标,如建议的here。但是当我尝试从光标中获取数据时,它似乎是空白的并且不执行循环内部的内容。我通过在循环内打印来证实这一点。
这是存储过程:
DELIMITER //
DROP PROCEDURE IF EXISTS `rd22` ;
CREATE PROCEDURE rd22(IN unitList varchar(100), OUT dur INT)
BEGIN
DECLARE c, done INT;
DECLARE cursor_end CONDITION FOR SQLSTATE '02000';
DECLARE v_column_val VARCHAR(50);
DECLARE cur_table CURSOR FOR select * from abet_view;
DECLARE CONTINUE HANDLER FOR cursor_end SET done = 1;
set @ul = unitList;
set @qry = concat("CREATE VIEW abet_view as SELECT abet.duration ",
"from active_begin_end_times as abet INNER JOIN alarm_sources as a ON a.id = abet.alarm_source ",
"and a.unit IN(",@ul,") WHERE abet.begin_timestamp = 1395874800000");
DROP VIEW abet_view;
prepare stmt from @qry;
execute stmt;
OPEN cur_table;
FETCH cur_table INTO v_column_val;
WHILE done = 0 DO
SET c = c + v_column_val;
FETCH cur_table INTO v_column_val;
END WHILE;
CLOSE cur_table;
SET dur = @c;
END//
DELIMITER ;
SET @val = '1,2,3';
CALL rd22(@val, @dur);
我是第一次编写存储过程,所以任何帮助都是值得的。
干杯!