当您尝试运行查询并看到错误#1064时 - 您的SQL语法中有错误;查看与您的MariaDB服务器版本对应的手册,以获得在''附近使用的正确语法。在第5行告诉我如何修复
CREATE FUNCTION CreateGroup(GroupName VARCHAR(50), GroupIcon TEXT, GroupDescription VARCHAR(130), GroupCreator INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE groupID INT;
INSERT INTO groups (name, icon, description) VALUES (GroupName, GroupIcon, GroupDescription);
SET groupID = LAST_INSERT_ID();
INSERT INTO group_members VALUES (groupID, GroupCreator);
RETURN groupID;
END;
CREATE FUNCTION AddMessage(r VARCHAR(255), s INT, t INT, m VARCHAR(255), creation DATETIME)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lastID INT;
DECLARE receiver INT;
SELECT id INTO receiver from users WHERE username=r;
INSERT INTO messages (receiver_id, sender_id, msg_type, message, created_At) values(receiver, s, t, m, creation);
SET lastID = LAST_INSERT_ID();
INSERT INTO messages_receipt (message_id, user_id, is_delivered) VALUES (lastID, receiver, 0);
RETURN lastID;
END;
CREATE FUNCTION AddGroupMessage(gid INT, s INT, t INT, m VARCHAR(255), creation DATETIME)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lastID INT; DECLARE rowCount INT;
SELECT COUNT(*) INTO rowCount FROM group_members WHERE user_id = s AND group_id = gid;
IF rowCount = 1 THEN
INSERT INTO group_messages (group_id, user_id, msg_type, message, created_at) VALUES (gid, s, t, m, creation);
SET lastID = LAST_INSERT_ID();
INSERT INTO group_receipt (message_id, user_id, is_delivered) SELECT gm.message_id, gmembers.user_id, 0 FROM
group_messages gm LEFT JOIN group_members gmembers ON gmembers.group_id = gm.group_id WHERE gm.group_id = gid AND gm.message_id = lastID AND NOT gmembers.user_id = s;
RETURN lastID;
ELSE
return 0;
END IF;
END;
CREATE DEFINER = 'inikspbh_weeki'@'localhost'
FUNCTION AddGroupMember(GroupID INT, MemberName VARCHAR(255), Username INT)
RETURNS int(11)
BEGIN
INSERT INTO group_members VALUES (GroupID, MemberName);
RETURN 1;
END;
答案 0 :(得分:0)
您必须使用delimiter
来分隔函数的创建
DELIMITER $$
DROP FUNCTION IF EXISTS `CreateGroup` $$
CREATE FUNCTION CreateGroup(GroupName VARCHAR(50), GroupIcon TEXT, GroupDescription VARCHAR(130), GroupCreator INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE groupID INT;
INSERT INTO groups (name, icon, description) VALUES (GroupName, GroupIcon, GroupDescription);
SET groupID = LAST_INSERT_ID();
INSERT INTO group_members VALUES (groupID, GroupCreator);
RETURN groupID;
END;
DROP FUNCTION IF EXISTS `AddMessage` $$
CREATE FUNCTION AddMessage(r VARCHAR(255), s INT, t INT, m VARCHAR(255), creation DATETIME)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lastID INT;
DECLARE receiver INT;
SELECT id INTO receiver from users WHERE username=r;
INSERT INTO messages (receiver_id, sender_id, msg_type, message, created_At) values(receiver, s, t, m, creation);
SET lastID = LAST_INSERT_ID();
INSERT INTO messages_receipt (message_id, user_id, is_delivered) VALUES (lastID, receiver, 0);
RETURN lastID;
END;$$
DROP FUNCTION IF EXISTS `AddGroupMessage` $$
CREATE FUNCTION AddGroupMessage(gid INT, s INT, t INT, m VARCHAR(255), creation DATETIME)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lastID INT; DECLARE rowCount INT;
SELECT COUNT(*) INTO rowCount FROM group_members WHERE user_id = s AND group_id = gid;
IF rowCount = 1 THEN
INSERT INTO group_messages (group_id, user_id, msg_type, message, created_at) VALUES (gid, s, t, m, creation);
SET lastID = LAST_INSERT_ID();
INSERT INTO group_receipt (message_id, user_id, is_delivered) SELECT gm.message_id, gmembers.user_id, 0 FROM
group_messages gm LEFT JOIN group_members gmembers ON gmembers.group_id = gm.group_id WHERE gm.group_id = gid AND gm.message_id = lastID AND NOT gmembers.user_id = s;
RETURN lastID;
ELSE
return 0;
END IF;
END;$$
DROP FUNCTION IF EXISTS `AddGroupMember` $$
CREATE DEFINER = 'inikspbh_weeki'@'localhost' FUNCTION AddGroupMember(GroupID INT, MemberName VARCHAR(255), Username INT)
RETURNS int(11)
BEGIN
INSERT INTO group_members VALUES (GroupID, MemberName);
RETURN 1;
END;$$
delimiter ;