这个
CREATE FUNCTION getTagID(tag VARCHAR(100)) RETURNS BIGINT
BEGIN
DECLARE tagID BIGINT;
SET tagID = (SELECT id FROM tags WHERE text = tag);
IF tagID IS NULL
THEN
(INSERT INTO tags (text) VALUES (tag));
SET tagID = (SELECT LAST_INSERT_ID());
END IF;
RETURN tagID;
END
失败并显示
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 4
为什么?
此函数与此处的问题相关:
最佳答案
尝试更改 DELIMITER
并删除 INSERT
语句两边的括号。
DELIMITER $$
CREATE FUNCTION getTagID(tag VARCHAR(100))
RETURNS BIGINT
BEGIN
DECLARE tagID BIGINT;
SET tagID = (SELECT id FROM tags WHERE text = tag);
IF tagID IS NULL THEN
INSERT INTO tags (text) VALUES (tag); -- don't put inside parenthesis
SET tagID = (SELECT LAST_INSERT_ID());
END IF;
RETURN tagID;
END $$
DELIMITER ;
关于MySQL创建函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548492/