在MYSQL中创建函数错误

时间:2014-04-27 22:05:39

标签: mysql function mysql-error-1064

我有这个错误: 错误代码:1064。您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在第19行附近使用正确的语法

DELIMITER $$
CREATE FUNCTION cadastrar_usuario(email VARCHAR(100), senha VARCHAR(30), nome VARCHAR(100), sobrenome VARCHAR(100)) RETURNS INT
BEGIN
DECLARE ID INT;
DECLARE EXISTE_EMAIL INT;

IF (email = '') || (senha = '') || (nome = '') || (sobrenome = '') THEN
    RETURN 1;

SELECT LAST_INSERT_ID() INTO ID;

SELECT COUNT(CODUSUARIO) FROM USUARIO_EMAIL WHERE EMAIL = email INTO EXISTE_EMAIL;
IF EXISTE_EMAIL > 0 THEN
    RETURN 2;

INSERT INTO USUARIO VALUES(0, senha, nome, sobrenome);
INSERT INTO USUARIO_EMAIL VALUES(ID, email, NULL);

RETURN 0;
  **END$$** This is the line 19

    DELIMITER ;

我不知道,问题是什么。

谢谢。

1 个答案:

答案 0 :(得分:1)

您忘了关闭IF块:

DROP FUNCTION IF EXISTS cadastrar_usuario;

DELIMITER $$
CREATE FUNCTION cadastrar_usuario(email VARCHAR(100), senha VARCHAR(30), 
    nome VARCHAR(100), sobrenome VARCHAR(100)) RETURNS INT
BEGIN
DECLARE ID INT;
DECLARE EXISTE_EMAIL INT;

IF (email = '') || (senha = '') || (nome = '') || (sobrenome = '') THEN
    RETURN 1;
END IF;

SELECT LAST_INSERT_ID() INTO ID;

SELECT COUNT(CODUSUARIO) FROM USUARIO_EMAIL WHERE EMAIL = email INTO EXISTE_EMAIL;

IF EXISTE_EMAIL > 0 THEN
    RETURN 2;
END IF;

INSERT INTO USUARIO VALUES(0, senha, nome, sobrenome);
INSERT INTO USUARIO_EMAIL VALUES(ID, email, NULL);

RETURN 0;

END

$$

DELIMITER ;

(测试并使用MySQL 5.6.12社区)。