使用带有IF SELECT的电子邮件参数的MySQL CREATE FUNCTION作为返回

时间:2014-08-19 13:05:49

标签: mysql

使用以下功能时收到以下错误,无法弄清楚如何修复它。

错误:

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 ';
  RETURN IF(result IS NOT NULL, result, '0');
  END' at line 8

功能:

delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
    DECLARE result VARCHAR(255);
    SET result = NULL;
    IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
    ENDIF;
    RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;

电子邮件将作为varchar或文本读入,例如“user@domain.com”

expiration_date是TIMESTAMP类型

payer_email的类型为VARCHAR(255)

我还尝试了以下功能,并显示了不同的错误。

错误:

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 ';
    READS SQL DATA
    BEGIN
        DECLARE result TIMESTAMP;
        SET res' at line 2

功能:

delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS TIMESTAMP;
READS SQL DATA
BEGIN
    DECLARE result TIMESTAMP;
    SET result = NULL;
    IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
    ENDIF;
    RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;

我想使用这些功能中的任何一个,以便应用程序可以直接传递到期(电子邮件);如果电子邮件的长度为9或更高,或者没有收到任何结果,则会得到结果。

更新:

谢谢KAILLINGER。花了2个小时研究这个,这是一个错字! :(

正确答案的完整性:

delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(200)
READS SQL DATA
BEGIN
    DECLARE result VARCHAR(200);
    SET result = NULL;
    IF (LENGTH(email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
    END IF;
    RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;

1 个答案:

答案 0 :(得分:0)

ENDIF;不好,应该是END IF;:)