title: Unable to import MYSQL function - ERROR 1064 (42000)
body: <p>I'm trying to understand why the following is failing and I'm not able to see it. The phpMyAdmin on our development server generated this exactly, but when I try importing it - I'm running into a weird parsing(?) error.</p>
  
  <pre><code>mysql&gt; DELIMITER $$
  mysql&gt;
  mysql&gt; --
  mysql&gt; -- Functions
  mysql&gt; --
  mysql&gt; DROP FUNCTION IF EXISTS `get_class_nextsession`$$
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  
  mysql&gt; CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
      -&gt;     READS SQL DATA
      -&gt; BEGIN
      -&gt; DECLARE nextsession,startdate,runningdate,enddate DATETIME;
      -&gt; SET nextsession= '-';
      -&gt; SELECT  MIN(CS.start_datetime) INTO startdate FROM site_class_schedule AS CS INNER JOIN site_class AS C ON (C.id=CS.class_id) WHERE CS.class_id=class_id Group by C.id Limit 0,1;
      -&gt; IF startdate &gt;= NOW() THEN
      -&gt; RETURN startdate;
      -&gt; ELSE
      -&gt; SELECT  CS.start_datetime INTO runningdate FROM site_class_schedule AS CS INNER JOIN site_class AS C ON (C.id=CS.class_id) WHERE CS.class_id=class_id and (NOW() between start_datetime and end_datetime ) Limit 0,1;
      -&gt; IF runningdate IS NOT NULL THEN
      -&gt; RETURN runningdate;
      -&gt; ELSE
      -&gt; SELECT  MAX(CS.end_datetime) INTO enddate FROM site_class_schedule AS CS INNER JOIN site_class AS C ON (C.id=CS.class_id) WHERE CS.class_id=class_id Group by C.id Limit 0,1;
      -&gt; IF enddate &lt; NOW() THEN
      -&gt; RETURN enddate;
      -&gt; ELSEIF enddate &gt;= NOW() THEN
      -&gt; SELECT  MIN(CS.start_datetime) INTO startdate FROM site_class_schedule AS CS INNER JOIN site_class AS C ON (C.id=CS.class_id) WHERE CS.class_id=class_id and CS.start_datetime &gt; NOW() Group by C.id Limit 0,1;
      -&gt;
  Display all 1866 possibilities? (y or n)
      -&gt;  startdate;
      -&gt;
  Display all 1866 possibilities? (y or n)
      -&gt; D IF;
      -&gt; END IF;
      -&gt; END IF;
      -&gt; RETURN nextsession;
      -&gt; END$$
  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 ';
  D IF;
  END IF;
  END IF;
  RETURN nextsession;
  END' at line 19
  mysql&gt;
  mysql&gt;
  </code></pre>
  
  <hr>
  
  <p>Progress Update 1</p>
  
  <p>This WORKED</p>
  
  <pre><code>mysql&gt; DELIMITER $$
  mysql&gt;
  mysql&gt; DROP FUNCTION IF EXISTS `get_class_nextsession`$$
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  
  mysql&gt; CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
      -&gt;     READS SQL DATA
      -&gt; BEGIN
      -&gt; RETURN nextsession;
      -&gt; END$$
  Query OK, 0 rows affected (0.00 sec)
  </code></pre>
  
  <p>This WORKED</p>
  
  <pre><code>mysql&gt;
  mysql&gt; DROP FUNCTION IF EXISTS `get_class_nextsession`$$
  Query OK, 0 rows affected (0.00 sec)
  
  mysql&gt;
  mysql&gt; CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
      -&gt;     READS SQL DATA
      -&gt; BEGIN
      -&gt; DECLARE nextsession,startdate,runningdate,enddate DATETIME;
      -&gt; RETURN nextsession;
      -&gt; END$$
  Query OK, 0 rows affected (0.00 sec)
  </code></pre>
  
  <p>This did NOT WORK - Notice how I'm trying to simplify the query / statements into basic ones.</p>
  
  <pre><code>mysql&gt; DROP FUNCTION IF EXISTS `get_class_nextsession`$$
  Query OK, 0 rows affected, 1 warning (0.01 sec)
  
  mysql&gt; CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
      -&gt;     READS SQL DATA
      -&gt; BEGIN
      -&gt; DECLARE nextsession,startdate,runningdate,enddate DATETIME;
      -&gt; SET nextsession= '-';
      -&gt; SELECT  MIN(start_datetime) INTO startdate FROM site_class_schedule;
      -&gt; IF startdate &gt;= NOW() THEN
      -&gt; RETURN startdate;
      -&gt; ELSE
      -&gt; END IF;
      -&gt; RETURN nextsession;
      -&gt; END$$
  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 'END IF;
  RETURN nextsession;
  END' at line 10
  </code></pre>