title: MySQL Stored Procedures : cursor declaration
body: <p>Sorry for the vague title, here is my problem. I have stored procedures for DB2 that i try to convert for MySQL. I'd like to know if i can write the SELECT statement in the cursor declaration as a string variable. For example with DB2 i have this :</p>
  
  <pre><code>(...)
  -- Declare cursors
  DECLARE c_very_init CURSOR WITH RETURN FOR s_very_init;
  DECLARE c_date      CURSOR WITH RETURN FOR s_date;
  
  DECLARE CONTINUE HANDLER FOR not_found
      SET at_end = 1;
  
  -- In case the_date is 0, retrieve the first date
  IF the_date = 0 THEN
      SET sql_end_date = '
          SELECT DATE 
              FROM ACCOUNTS 
          WHERE REF = ''' || the_ref || ''' 
              ORDER BY ID ASC FETCH FIRST 1 ROWS ONLY';
      PREPARE s_date FROM sql_end_date;
      OPEN c_date;
      FETCH FROM c_date INTO data_ins;
      SET the_last_date = data_ins;
      CLOSE c_date;
  ELSE
      SET the_last_date = the_date;
  END IF;
  
  -- Get the 'very' initial value
  SET sql_very_init = '
          SELECT in, out 
              FROM MOVEMENTS 
          WHERE REF = ''' || the_ref || ''' 
              AND DATE &lt; ' || the_last_date;
  PREPARE s_very_init FROM sql_very_init;
  OPEN c_very_init;
  FETCH FROM c_very_init INTO dare, avere;
  
  -- Loop through the results
  (...)
  </code></pre>
  
  <p>I declare a <code>c_very_init</code> cursor, but at the time of the cursor declaration in the SP i still don't know the full select statement because i need to fetch (if necessary) the <code>the_last_date</code> value. It seems i can't do this :</p>
  
  <pre><code>DECLARE c_very_init CURSOR WITH RETURN FOR s_very_init;
  </code></pre>
  
  <p>with MySQL, the syntax being with the statement directly in the declaration :</p>
  
  <pre><code>DECLARE c_very_init CURSOR FOR SELECT blaablaa...;
  </code></pre>
  
  <p>Am i wrong?</p>
  
  <p>Thank you.
  fabien.</p>