MySQL存储了Proc语法混乱

时间:2015-09-07 22:07:30

标签: mysql stored-procedures

使用语法表单文档,阅读许多示例和文档,我减少了代码以隔离导致失败的语法,我看不到它......我仍然收到错误。

通过MySQL命令行运行

PARAMS:A_score smallint,B_score smallint

delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
    DECLARE  winner BIGINT DEFAULT 0;
    DECLARE  winningScore, losingScore SMALLINT DEFAULT;
    if A_score > B_score then
        SET winningScore = 1;
    elseif A_score < B_score then
        SET winningScore = 2;
    end if;
    start transaction;
        UPDATE
            winners
        SET
            winner = winningScore 
        WHERE
            id = 1
    commit;
end $$
delimiter ;

错误1064(42000):您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便在&#39 ;;附近使用正确的语法;     如果A_score&gt;那么B_score         SET winningScore = 1;     elseif A_score&#39;在第4行

1 个答案:

答案 0 :(得分:1)

我发现你忘记写SET(在你问题的上一个版本中)为你的变量赋值。

(我将winningScorelosingScore的类型更改为字符,因为smallint s不能成为字符串):

-- Be sure to change the default delimiter before writing your procedure
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
    DECLARE  winner BIGINT DEFAULT 0;
    DECLARE  winningScore, losingScore VARCHAR(2) DEFAULT;
    if A_score > B_score then
        SET winningScore = 'A';
    --  ^^^--you forgot this
    elseif A_score < B_score then
        SET winningScore = 'B';
    --  ^^^--and this
    else
        SET winningScore = 'AB';
    --  ^^^--and this
    end if;
    start transaction;
    -- Do whatever your transaction is meant to be
    commit;
end $$
--  ^^--- And I think you're forgetting to put this

-- Be sure to reset the delimiter to ; after you end your procedure
delimiter ;

引自the reference manual

  

可以使用SET语句直接设置变量。 See Section 13.7.4, “SET Syntax”

希望这有帮助