gpt4 book ai didi

mysql - 无法在 MYSQL 中创建函数错误 1064 (42000)

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:54 24 4
gpt4 key购买 nike

您好,我正在使用 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// 将其从 ; 更改

最佳答案

从声明中删除引号

DECLARE idGroup INT;

而且您忘记了函数开头的分隔符更改

delimiter //
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))

你忘了结束你的if语句

end if;

任何你想结束的地方

关于mysql - 无法在 MYSQL 中创建函数错误 1064 (42000),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13384581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com