title: MySQL Cursor Not Working
body: <p>I am going nuts about this problem.</p>
  
  <p>The stored procedure below opens a cursor, and then iterates over the result to do some inserts.</p>
  
  <p>The select statement used for the cursor works when used outside a cursor (in the sql window). However it does not <em>work</em> in the cursor, the cursor returns NULL when opened!</p>
  
  <p>Do note that this code is in a stored proc., that is itself called by another stored procedure which is opening another cursor. Not sure if this info. is useful.</p>
  
  <pre><code>DECLARE location VARCHAR(255);                      -- location
  DECLARE isLocationDone BOOL DEFAULT FALSE;   -- status flag
  DECLARE curLocation CURSOR FOR
      SELECT `LOCATION` AS loc FROM `mfdtemp`.`opexstk_tmp`
      WHERE `client` = in_client AND DATE(`rentdat`) &lt; in_date
      ORDER BY loc;
  DECLARE 
      CONTINUE HANDLER FOR SQLSTATE '02000' SET isLocationDone = TRUE;
  
  OPEN curLocation;
  
  label_location_loop: LOOP
  
      FETCH curLocation INTO location;
  
      IF isLocationDone THEN
          CLOSE curLocation;
          LEAVE label_location_loop;
      END IF;
  
  
      INSERT INTO t01_stock_report_htry (
          t01date, t01client, t01desc 
      ) 
      VALUES (in_date, in_client, CONCAT_WS('', 'Stack ', location)
      );
  
  END LOOP;
  </code></pre>