Dynamic cursor using a view in mysql
I am trying to write a dynamic cursor in a stored procedure using a cursor as suggested here . But when I try to fetch data from the cursor, it appears to be empty and does not execute what is inside the loop. I confirmed this by typing inside the loop.
This is a stored procedure:
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);
This is my first time writing a stored procedure, so any help is noticeable.
Hooray!
+3
source to share
No one has answered this question yet
Check out similar questions: