需要Mysql存储功能帮助

时间:2011-05-06 10:38:43

标签: mysql stored-procedures syntax

我刚开始学习mysql中存储的函数。有人可以告诉我下面有什么不对吗?

Phpmyadmin说你的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'return NAME附近使用正确的语法; 结束//'第19行

DELIMITER //
DROP FUNCTION IF EXISTS getName //
CREATE FUNCTION getName(type CHAR(10), id int) RETURNS CHAR(50) DETERMINISTIC
BEGIN
    DECLARE NAME CHAR(50);
    CASE type
        WHEN 'offer' THEN 

        SELECT Type_Name INTO NAME FROM otypes WHERE Type_Id = id;          

        WHEN 'service' THEN 

        SELECT ServiceType_Name INTO NAME FROM stypes WHERE ServiceType_Id = id;    

        WHEN 'store' THEN 

        SELECT Store_Name INTO NAME FROM stores WHERE Store_Id = id;    

    END CASE

    return NAME;
END //

2 个答案:

答案 0 :(得分:1)

END CASE之后加上分号:

DELIMITER //
DROP FUNCTION IF EXISTS getName //
CREATE FUNCTION getName(type CHAR(10), id int) RETURNS CHAR(50) DETERMINISTIC
BEGIN
    DECLARE NAME CHAR(50);
    CASE type
        WHEN 'offer' THEN 

        SELECT Type_Name INTO NAME FROM otypes WHERE Type_Id = id;          

        WHEN 'service' THEN 

        SELECT ServiceType_Name INTO NAME FROM stypes WHERE ServiceType_Id = id;    

        WHEN 'store' THEN 

        SELECT Store_Name INTO NAME FROM stores WHERE Store_Id = id;    

    END CASE;
--          ^

    return NAME;
END //

答案 1 :(得分:0)

您错过了;

END CASE

return NAME;

应该是

END CASE;

return NAME;