views:

19

answers:

1

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.

mysql> DELIMITER $$
mysql>
mysql> --
mysql> -- Functions
mysql> --
mysql> DROP FUNCTION IF EXISTS `get_class_nextsession`$$
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
    ->     READS SQL DATA
    -> BEGIN
    -> DECLARE nextsession,startdate,runningdate,enddate DATETIME;
    -> SET nextsession= '-';
    -> 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;
    -> IF startdate >= NOW() THEN
    -> RETURN startdate;
    -> ELSE
    -> 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;
    -> IF runningdate IS NOT NULL THEN
    -> RETURN runningdate;
    -> ELSE
    -> 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;
    -> IF enddate < NOW() THEN
    -> RETURN enddate;
    -> ELSEIF enddate >= NOW() THEN
    -> 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 > NOW() Group by C.id Limit 0,1;
    ->
Display all 1866 possibilities? (y or n)
    ->  startdate;
    ->
Display all 1866 possibilities? (y or n)
    -> D IF;
    -> END IF;
    -> END IF;
    -> RETURN nextsession;
    -> 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>
mysql>

Progress Update 1

This WORKED

mysql> DELIMITER $$
mysql>
mysql> DROP FUNCTION IF EXISTS `get_class_nextsession`$$
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
    ->     READS SQL DATA
    -> BEGIN
    -> RETURN nextsession;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

This WORKED

mysql>
mysql> DROP FUNCTION IF EXISTS `get_class_nextsession`$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
    ->     READS SQL DATA
    -> BEGIN
    -> DECLARE nextsession,startdate,runningdate,enddate DATETIME;
    -> RETURN nextsession;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

This did NOT WORK - Notice how I'm trying to simplify the query / statements into basic ones.

mysql> DROP FUNCTION IF EXISTS `get_class_nextsession`$$
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
    ->     READS SQL DATA
    -> BEGIN
    -> DECLARE nextsession,startdate,runningdate,enddate DATETIME;
    -> SET nextsession= '-';
    -> SELECT  MIN(start_datetime) INTO startdate FROM site_class_schedule;
    -> IF startdate >= NOW() THEN
    -> RETURN startdate;
    -> ELSE
    -> END IF;
    -> RETURN nextsession;
    -> 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
A: 

Ah ha!

Definitely an 'unusual' error wherein 'nothing' was wrong! The problems I was facing was due to excessive tabs being generated or placed in the original FUNCTION creation! The two consecutive \t s were breaking the flow of data input on the mysql prompt and having mysql ask

 Display all 1866 possibilities? (y or n)

Moreover, when I was copy pasting here on SO, I was pasting the final output of my interaction with the MYSQL command prompt, not what I had actually placed 'in' there!

DROP FUNCTION IF EXISTS `get_class_nextsession`$$
CREATE DEFINER=`myusername`@`localhost` FUNCTION `get_class_nextsession`(class_id INT) RETURNS datetime
    READS SQL DATA
BEGIN
DECLARE nextsession,startdate,runningdate,enddate DATETIME;
SET nextsession= '-';
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;
IF startdate >= NOW() THEN
RETURN startdate;
ELSE    
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;
IF runningdate IS NOT NULL THEN
RETURN runningdate;
ELSE
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;
IF enddate < NOW() THEN
    RETURN enddate;
ELSEIF enddate >= NOW() THEN
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 > NOW() Group by C.id Limit 0,1;
            RETURN startdate; ****ISSUE WITH MORE THAN 2 TABS****
        END IF; ****ISSUE WITH MORE THAN 2 TABS****
    END IF;
END IF;
RETURN nextsession;
END$$
matt_tm