title: Prepared IF block in stored procedure
body: <p>I was testing out MySQL's <code>PREPARE stmt FROM preparable_stmt;</code> functionality in a stored procedure and decided to try out control blocks, starting with <code>IF</code>:</p>
  
  <pre><code>DELIMITER $$
  CREATE PROCEDURE `testif`(t TEXT) BEGIN
      DECLARE q TEXT;
      SET @q:=CONCAT("IF (SELECT COUNT(*) FROM ", t, ")&gt;5 THEN");
      PREPARE query FROM @q; EXECUTE query; 
          DEALLOCATE PREPARE query;
          SELECT "That's a nice table!";
          INSERT INTO ... #and other behaviour other than SELECT
      SET @q:="END IF";
      PREPARE query FROM @q; EXECUTE query; DEALLOCATE PREPARE query;
  END$$
  </code></pre>
  
  <p>Since it was being executed in the context of a stored procedure, I kind of expected this to work. Instead, when I <code>CALL testif("t1");</code>, I get this syntax error:</p>
  
  <pre><code>ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF (SELECT COUNT(*) FROM t1)&gt;5 THEN' at line 1
  </code></pre>
  
  <p>which I would get if I ran the query outside of the procedure.</p>
  
  <p><em>For the record, I know how to make this query work without <code>PREPARE</code>ing the IF clause directly.</em> My question is purely out of curiosity, since I'm used to how <code>eval</code>-like functions work in other programming languages (where it executes the string as if it were hard-coded on that line). Setting variables and most other things work with <code>PREPARE</code>, but then again, they'd work outside of the procedure as well. Why doesn't this?</p>