简体   繁体   English

创建MySQL函数时出错(1064)

[英]Error creating MySQL function (1064)

phpMyAdmin: 3.5.7
MySQL: 5.5.29

I want users to be able to compile several different lists from posts in the database. 我希望用户能够从数据库中的帖子中编译几个不同的列表。 The following function is supposed to be used to add a post to one such list. 应该使用以下功能将帖子添加到这样的列表中。 It checks if the list ID supplied by the user matches with the given user id, then checks if the list is not full, then adds post to list. 它检查用户提供的列表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 ;

I enter this code into phpMyAdmin's SQL console, it returns 我将此代码输入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

Now, should I move the "faulty" line up, the code looks like this: 现在,我应该将“ faulty”行向上移动,代码如下所示:

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);
[...]

This version points to the same error, but on another line: 此版本指向相同的错误,但在另一行:

#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

This leads me to believe the problem might be caused by faulty use of the delimiter, but that is all I have got right now. 这使我相信问题可能是由于分隔符使用不当引起的,但这就是我现在所掌握的一切。 PhpMyAdmin has a delimiter field after the SQL insertion field, which I have tried leaving empty, populating with ; PhpMyAdmin在SQL插入字段之后有一个定界符字段,我尝试将其保留为空,并填充; and with // , none of which made a difference. // ,没有任何改变。

What might cause this issue, and how do I fix it? 是什么导致此问题,如何解决?

You should keep variable declarations at the top. 您应该将变量声明放在顶部。 Also I took the freedom to rewrite some code: 我也可以自由地重写一些代码:

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 ;

Don't use COUNT(*) if you only want to know if something exists. 如果只想知道是否存在某些东西,请不要使用COUNT(*)

Try below code, In MySQL , while using ELSE IF Code there doesn't given space between ELSE and IF`. 尝试下面的代码,在MySQL ,当使用ELSE IF Code there doesn't given space between ELSE and IF` Code there doesn't given space between Also Declare variables at the top 同时在顶部声明变量

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM