title: MySQL: IF / THEN statements in stored procedures
body: <p>I'm writing a stored procedure that uses multiple IF / THEN statements that also need to execute multiple queries if they evaluate to true.  Problem is, I can't seem to find any examples of the appropriate syntax.  From the <a href="http://dev.mysql.com/doc/refman/5.0/en/if-statement.html">MySQL dev handbook</a>, it seems like I could have multiple queries in the "statement_list," but so far I can't get it to work.</p>
  
  <p>Here's what I'm trying to do:</p>
  
  <pre><code>SET agency = 
    COALESCE((SELECT org_agency_o_id 
              FROM orgs_agencies 
              WHERE org_agency_code = maj_agency_cat)
                ,(SELECT min(org_id) 
                  FROM orgs     
                  WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5)))) 
  
  IF agency IS NULL THEN 
    -- execute multiple queries   
    INSERT INTO orgs (org_name
                     ,org_name_length
                     ,org_type
                     ,org_sub_types) 
    VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))
           ,LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
           ,'org','Org,GovernmentEntity,Federal,Agency');
  
  SET agency = LAST_INSERT_ID();
  END IF;
  </code></pre>
  
  <p>The error:</p>
  
  <blockquote>
    <p>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 <code>'IF agency IS NULL THEN 
                INSERT INTO orgs (org_name,org_name_length,org_type,'</code> at line 53</p>
  </blockquote>
  
  <p>Any ideas?  I know it has to be something simple, so I would greatly appreciate anybody's input.</p>