title: mysql stored procedure set value from table
body: <p>I've got a following, simple table:</p>
  
  <pre><code>mysql&gt; select * from version;
  +----+---------+
  | id | version |
  +----+---------+
  |  1 | 1       |
  +----+---------+
  1 row in set (0.00 sec)
  </code></pre>
  
  <p>I need to create a stored procedure that will do something (or nothing) depending on the value of this table (precisely, the only row of this table).</p>
  
  <pre><code>DELIMITER $$
  DROP PROCEDURE IF EXISTS upgrade_version $$
  CREATE PROCEDURE upgrade_version(current_version INTEGER, script TEXT)
  BEGIN
    DECLARE version INT(11);
    SET version = (SELECT `version` FROM `version` WHERE `id` = 1 LIMIT 1);
    SELECT version;
    IF version = current_version + 1 THEN
      PREPARE upgrade_stmt FROM script;
      EXECUTE upgrade_stmt;
      UPDATE `version` SET `version` = `version` + 1 WHERE `id` = 1;
    ENDIF;
  END $$
  DELIMITER ;
  </code></pre>
  
  <p>To be called within:</p>
  
  <pre><code>CALL upgrade_version(1,'ALTER TABLE ...');
  </code></pre>
  
  <p>It is syntactically incorrect, mysql returns:</p>
  
  <pre><code>1064 - 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 'script;
      EXECUTE upgrade_stmt;
      UPDATE `version` SET `version` = `version`' at line 7 
  </code></pre>
  
  <p>What is wrong?</p>