无法创建存储函数 - 语法错误?

发布于 2024-12-03 07:24:51 字数 783 浏览 1 评论 0原文

我有以下问题。

我想创建一个存储函数,将实体 ID 转换为相应的 sku

这是代码:

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    return returnvalue;
END

现在我的问题是,如果我触发查询,我会收到以下消息: [窗口标题] 错误

SQL Error (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 'LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT' at line 3

我使用的数据库是 MySQL 5.0.51a

提前感谢您的想法。

I've got the following Problem.

I want to create a stored function which converts an entity_id into the corresponding sku

Here's the code:

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    return returnvalue;
END

Now my problem is if i fire the query i get the following message:
[Window Title]
Error

SQL Error (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 'LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT' at line 3

The db im using is MySQL 5.0.51a

Thanks in advance for your ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打 2024-12-10 07:24:51

MySQL 默认使用 ; 作为分隔符,因此当它在第 9 行遇到 ; 时:

 DECLARE returnvalue varchar(50);

MySQL 认为命令结束 - 它正在尝试执行:

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

这是无效的SQL。

设置新的分隔符:

 DELIMITER $

 CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    return returnvalue;
END
$

然后您可以使用以下命令将分隔符更改回来:

DELIMITER ;

MySQL by default uses ; as a delimiter, so when it encounters the ; at line 9:

 DECLARE returnvalue varchar(50);

MySQL thinks the command ends - it is trying to execute:

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

which isn't valid SQL.

Set a new delimiter:

 DELIMITER $

 CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue varchar(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    return returnvalue;
END
$

You can then change the delimiter back with:

DELIMITER ;
梦屿孤独相伴 2024-12-10 07:24:51

这应该有效:

DELIMITER $

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue VARCHAR(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    RETURN returnvalue;

    END$

DELIMITER ;

您没有指定 varchar 长度。 :-)

This should work:

DELIMITER $

CREATE FUNCTION `id2sku`(`entity_id_in` INT)
    RETURNS VARCHAR(50)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE returnvalue VARCHAR(50);

    SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;

    RETURN returnvalue;

    END$

DELIMITER ;

You had not specified the varchar length. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文