简体   繁体   English

在MySQL中运行函数时出错

[英]Error when Running a function in MySQL

I successfully created the following function in mysql 我在mysql中成功创建了以下函数

CREATE FUNCTION h2m
HTMLText(text)
RETURNS text
DETERMINISTIC
BEGIN
DECLARE Start INT;
DECLARE End INT;
DECLARE Length INT;
SET Start = CHARINDEX('<',HTMLText);
SET End = CHARINDEX('>',HTMLText,CHARINDEX('<',HTMLText));
SET Length = (End - Start) + 1;
WHILE Start > 0
AND End > 0
AND Length > 0
DO
SET HTMLText = STUFF(HTMLText,Start,Length,'');
SET Start = CHARINDEX('<',HTMLText);
SET End = CHARINDEX('>',HTMLText,CHARINDEX('<',HTMLText));
SET Length = (End - Start) + 1;
END WHILE;
RETURN LTRIM(RTRIM(HTMLText));
END;

Now when I'm trying to call it with the following code: 现在,当我尝试使用以下代码进行调用时:

SELECT description h2m (HTMLText) FROM oc_product_description,

I am getting the following 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 in '(HTML Text) FROM oc product_description LIMIT 0, 30' at line 2 –

Thank you so much for your help 非常感谢你的帮助

You can try: 你可以试试:

SELECT description, h2m(HTMLText)
FROM oc_product_description

Three things: 三件事:

  • Comma after description . description后用逗号description
  • Removing space between h2m and ( . MySQL has a problem with spaces after function names. 删除h2m( 。之间的h2m 。MySQL在函数名称后h2m空格。
  • Removal of comma after oc_product_description . 删除oc_product_description之后的逗号。

EDIT: 编辑:

Perhaps you want to pass description in as the argument: 也许您想将description作为参数传递:

SELECT h2m(description)
FROM oc_product_description

You have two problems. 你有两个问题。 First the CHARINDEX() function isn't a built-in MySQL function. 首先, CHARINDEX()函数不是内置的MySQL函数。 Unless you have it custom defined somewhere in your DB, you'll run into an error here. 除非您在数据库的某个位置自定义定义,否则在这里会遇到错误。 LOCATE() is the built-in MySQL function that does something similar to what CHARINDEX() does. LOCATE()是内置的MySQL函数,其功能与CHARINDEX()相似。

Your second problem is the way you're calling the function. 第二个问题是调用函数的方式。 You should be calling it in this fashion 你应该这样称呼它

SELECT h2m (description) 
FROM oc_product_description

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM