我在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;
现在,当我试图用以下代码调用它时:
SELECT description h2m (HTMLText) FROM oc_product_description,
我得到以下错误:
#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 –
非常感谢你的帮助
最佳答案
你有两个问题。首先,CHARINDEX()
函数不是内置的MySQL函数。除非在数据库中自定义了它,否则在这里会遇到错误。LOCATE()
是一个内置的MySQL函数,它的功能与CHARINDEX()
类似。
第二个问题是调用函数的方式。你应该这样称呼它
SELECT h2m (description)
FROM oc_product_description