您好我正在使用MySQL创建一个函数:
CREATE FUNCTION INSERTGROUP(name VARCHAR(50))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE 'idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO 'idGroup' FROM groupsTable WHERE groupName='name';
RETURN 'idGroup';
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
''idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM serverGroupsTable WHERE gro' at line 5
我经历过与此类似的论坛和其他问题,但我无法创建该功能。
我做错了什么?语法是否正确?我需要添加其他内容吗?
好的,这就是我的尝试:
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = name))
THEN INSERT INTO groupsTable (groupName) VALUES (name);
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName=name;
RETURN idGroup;
END//
和
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName='name';
RETURN idGroup;
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
'' at line 13
与上面的相似
我也DELIMITER //
从;
答案 0 :(得分:4)
从声明中删除引号
DECLARE idGroup INT;
你忘记了函数开头的分隔符更改
delimiter //
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
您忘了结束if
声明
end if;
无论你希望它结束