title: Dynamic cursor using a view in mysql
body: <p>I am trying to write a dynamic cursor in a stored procedure using a cursor, as suggested <a href="http://forums.mysql.com/read.php?61,116597,226041" rel="nofollow">here</a>. But when I try to fetch data from the cursor it seems to be blank and does not executed what is inside the loop. I confirmed this by printing inside the loop.</p>
  
  <p>This is the stored procedure:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I am writing the stored procedure for the first time, so any help is appreciable.</p>
  
  <p>Cheers!</p>