phpMyAdmin: 3.5.7
MySQL: 5.5.29
我希望用户能够从数据库中的帖子编译几个不同的列表。应该使用以下函数将帖子添加到一个这样的列表中。它检查用户提供的列表ID是否与给定的用户ID匹配,然后检查列表是否已满,然后将帖子添加到列表中。
DELIMITER //
CREATE FUNCTION addToFav(inputKid INT, inputUid INT, listID INT, listLimit INT)
RETURNS VARCHAR(300)
BEGIN
DECLARE resultMsg VARCHAR(300);
DECLARE listExists INT; SET listExists = (SELECT COUNT(*) FROM fav_lists WHERE fav_list_id=listID AND uid=inputUid AND active=1);
IF 1 > listExists THEN SET resultMsg = 'LIST NON-EXISTANT';
ELSE
DECLARE listSize INT;
SET listSize = (SELECT COUNT(*) FROM fav_links WHERE fav_list_id=listID AND active=1);
IF listSize = listLimit THEN SET resultMsg = 'LIST FULL';
ELSE IF listSize > listLimit THEN SET resultMsg ='dbErr: LIST OVER LIMIT';
ELSE
REPLACE INTO fav_links (kid, fav_list_id, active) VALUES (inputKid, listID, 1);
SET resultMsg ='SUCCESS';
END IF;
END IF;
RETURN resultMsg;
END //
DELIMITER ;
我将此代码输入phpMyAdmin的SQL控制台,它返回
#1064 - 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 'DECLARE listSize INT; SET listSize = (SELECT COUNT(*) FROM fav_links W' at line 10
现在,如果我移动“有缺陷”的行,代码看起来像这样:
DELIMITER //
CREATE FUNCTION addToFav(inputKid INT, inputUid INT, listID INT, listLimit INT)
RETURNS VARCHAR(300)
BEGIN
DECLARE listSize INT; SET listSize = (SELECT COUNT(*) FROM fav_links WHERE fav_list_id=listID AND active=1);
DECLARE resultMsg VARCHAR(300);
DECLARE listExists INT; SET listExists = (SELECT COUNT(*) FROM fav_lists WHERE fav_list_id=listID AND uid=inputUid AND active=1);
[...]
此版本指向相同的错误,但在另一行:
#1064 - 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 'DECLARE resultMsg VARCHAR(300); DECLARE listExists INT; SET listExists = (SELEC' at line 6
这让我相信问题可能是由于分隔符的错误使用造成的,但这就是我现在所拥有的。 PhpMyAdmin在SQL插入字段后面有一个分隔符字段,我尝试将其留空,填充;
和//
,但没有一个产生差异。
可能导致此问题的原因,以及如何解决?
答案 0 :(得分:2)
您应该将变量声明保留在顶部。我也自由地重写了一些代码:
DELIMITER //
CREATE FUNCTION addToFav(inputKid INT, inputUid INT, listID INT, listLimit INT)
RETURNS VARCHAR(300)
BEGIN
DECLARE resultMsg VARCHAR(300);
DECLARE listSize INT;
IF (NOT EXISTS (SELECT 1 FROM fav_lists WHERE fav_list_id=listID AND uid=inputUid AND active=1)) THEN
SET resultMsg = 'LIST NON-EXISTANT';
ELSE
SELECT COUNT(*) INTO listSize FROM fav_links WHERE fav_list_id=listID AND active=1;
IF (listSize = listLimit) THEN
SET resultMsg = 'LIST FULL';
ELSEIF (listSize > listLimit) THEN
SET resultMsg ='dbErr: LIST OVER LIMIT';
ELSE
BEGIN
REPLACE INTO fav_links (kid, fav_list_id, active) VALUES (inputKid, listID, 1);
SET resultMsg ='SUCCESS';
END;
END IF;
END IF;
RETURN resultMsg;
END //
DELIMITER ;
如果您只想知道某些事物是否存在,请不要使用COUNT(*)
。
答案 1 :(得分:1)
使用ELSE IF MySQL
ELSE Code there doesn't given space between
IF`时,在and
中尝试以下代码。还在顶部声明变量
DELIMITER //
CREATE FUNCTION addToFav(inputKid INT, inputUid INT, listID INT, listLimit INT) RETURNS VARCHAR(300)
BEGIN
DECLARE resultMsg VARCHAR(300);
DECLARE listExists INT;
DECLARE listSize INT;
SET listExists = (SELECT COUNT(*) FROM fav_lists WHERE fav_list_id=listID AND uid=inputUid AND active=1);
IF (1 > listExists) THEN
SET resultMsg = 'LIST NON-EXISTANT';
ELSE
SET listSize = (SELECT COUNT(*) FROM fav_links WHERE fav_list_id=listID AND active=1);
IF listSize = listLimit THEN
SET resultMsg = 'LIST FULL';
ELSEIF listSize > listLimit THEN
SET resultMsg ='dbErr: LIST OVER LIMIT';
ELSE
REPLACE INTO fav_links (kid, fav_list_id, active) VALUES (inputKid, listID, 1);
SET resultMsg ='SUCCESS';
END IF;
END IF;
RETURN resultMsg;
END //
DELIMITER ;