我遇到了以下问题。
我想创建一个存储函数,将entity_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
db im using是MySQL 5.0.51a
提前感谢您的想法。
答案 0 :(得分:7)
默认情况下,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 ;
答案 1 :(得分:2)
这应该有效:
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长度。 : - )