创建时的MySQL过程错误

时间:2014-03-24 14:38:20

标签: mysql procedure

在下面创建一个程序,它不断给出关于为第一个select语句赋值的信息,我完全错了,任何帮助都非常感激。

  

错误1064(42000):您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便在':= m id附近使用正确的语法,vCurrentVote:= vote FROM td_voted WHERE id = pCid LIMIT 1;

CREATE PROCEDURE pr_update_vote (IN pCid INT, IN pVote INT, IN pMid INT, OUT pNewVote INT)
BEGIN
    DECLARE vMid, vVote, vCurrentVote, vNewVote INT;

    IF pVote = 1 THEN
        SET vVote = 1;
    ELSE
        SET vVote = -1;
    END IF;

    SELECT vMid := `mid`, vCurrentVote := vote FROM td_voted WHERE id = pCid LIMIT 1;

    IF vMid <> pMid THEN
        SELECT vVoteType := vote 
        FROM td_votes 
        WHERE `mid` = vMid 
        AND cid = pCid 
        LIMIT 1;

        IF vVoteType NOT NULL OR vVoteType <> vVote THEN
            UPDATE td_votes 
            SET vote = vVote, 
            updated = NOW()
            WHERE cid = pCid
            AND `mid` = pMid
            LIMIT 1;
        ELSE
            INSERT INTO td_votes (`mid`, cid, vote)
            VALUES (pMid, pCid, vVote);
        END IF;

        vNewVote = vCurrentVote + vVote;

        UPDATE td_voted
        SET vote = vNewVote
        WHERE id = pCid
        LIMIT 1;

        SELECT pNewVote := vote FROM td_voted WHERE id = pCid LIMIT 1;
    END IF;
END

1 个答案:

答案 0 :(得分:0)

这是经过大量调试后对我有用的解决方案。

CREATE PROCEDURE pr_update_vote (IN pCid INT, IN pVote INT, IN pMid INT, OUT pNewVote INT)
BEGIN
    DECLARE vMid, vVote, vCurrentVote, vNewVote INT;

    IF pVote = 1 THEN
        SET vVote = 1;
    ELSE
        SET vVote = -1;
    END IF;

    SELECT vMid = `mid`, vCurrentVote = vote FROM td_voted WHERE id = pCid LIMIT 1;

    IF vMid <> pMid THEN
        SELECT vVoteType = vote 
        FROM td_votes 
        WHERE `mid` = vMid 
        AND cid = pCid 
        LIMIT 1;

        IF (vVoteType IS NOT NULL) OR (vVoteType <> vVote) THEN
            UPDATE td_votes 
            SET vote = vVote, 
            updated = NOW()
            WHERE cid = pCid
            AND `mid` = pMid
            LIMIT 1;
        ELSE
            INSERT INTO td_votes (`mid`, cid, vote)
            VALUES (pMid, pCid, vVote);
        END IF;

        SET vNewVote = vCurrentVote + vVote;

        UPDATE td_voted
        SET vote = vNewVote
        WHERE id = pCid
        LIMIT 1;

        SELECT pNewVote = vote FROM td_voted WHERE id = pCid LIMIT 1;
    END IF;
END