title: Using prepared statements with cursor
body: <p>I put the cursor declaration in the prepared statement and then executed it, then returns an <code>error #1324 - Undefined CURSOR: getid.</code></p>
  
  <p>How do I solve this problem?</p>
  
  <pre>
  delimiter ;;
  
  drop procedure if exists test2;;
  
  create procedure test2(table_id VARCHAR(25))
  BEGIN
      DECLARE done INT DEFAULT FALSE;
      DECLARE id INT;
      DECLARE id_new INT;
      DECLARE stmt1 VARCHAR(1024);
      DECLARE stmt2 VARCHAR(1024);
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  
      SET @sqltext1 := CONCAT('DECLARE getid CURSOR FOR SELECT entryId FROM ',table_id,' ORDER BY entryId');
      PREPARE stmt1 FROM @sqltext1;
      EXECUTE stmt1;
  
      SET @id_new = 1; 
  
      OPEN getid;
  
      FETCH getid into id;
      REPEAT
          SET @sqltext2 := CONCAT('UPDATE ',table_id,' SET entryId = ? WHERE entryId = ?');
          PREPARE stmt2 FROM @sqltext2;
          EXECUTE stmt2 USING @new_id, id;
          SET @id_new = @id_new + 1;
          FETCH getid into id;
      UNTIL done END REPEAT;
      CLOSE getid;
  END
  ;;
  
  CALL test2('Test');
  </pre>