MySQL - 创建用户定义函数 (UDF) 时出现问题

发布于 2024-11-18 22:04:24 字数 721 浏览 2 评论 0 原文

我正在尝试创建这个函数:

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC

BEGIN

  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';

  WHILE (i <= LENGTH(prm_strInput) )  DO
    SET v_char = SUBSTR(prm_strInput,i,1);

    IF v_char REGEXP '^[A-Za-z0-9]$' THEN
      SET v_parseStr = CONCAT(v_parseStr,v_char);  
    END IF;

    SET i = i + 1;
  END WHILE;

  RETURN trim(v_parseStr);
END

但是 MySQL 说:

13:52:45 [CREATE - 0 行,0.000 秒] [错误代码:1064,SQL 状态:42000] 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 5 行 '' 附近使用的正确语法

我可能错了什么?语法对我来说看起来是正确的。

I'm trying to create this function:

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC

BEGIN

  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';

  WHILE (i <= LENGTH(prm_strInput) )  DO
    SET v_char = SUBSTR(prm_strInput,i,1);

    IF v_char REGEXP '^[A-Za-z0-9]

But MySQL says:

13:52:45 [CREATE - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 42000] 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 '' at line 5

What could I being wrong? The syntax looks correct to me.

THEN SET v_parseStr = CONCAT(v_parseStr,v_char); END IF; SET i = i + 1; END WHILE; RETURN trim(v_parseStr); END

But MySQL says:

13:52:45 [CREATE - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 42000] 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 '' at line 5

What could I being wrong? The syntax looks correct to me.

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

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

发布评论

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

评论(3

泪眸﹌ 2024-11-25 22:04:24

您必须更改分隔符,以便可以在函数内使用 ;

DELIMITER $

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) )  DO
  SET v_char = SUBSTR(prm_strInput,i,1);
  IF v_char REGEXP '^[A-Za-z0-9]

MySQL 命令行客户端(以及许多其他 SQL 客户端)默认分隔符是 ;。因此,当您键入原始代码时,MySQL 认为第一个命令在找到第一个 ; 的地方结束(在第 5 行,如错误消息所述),因此您会收到错误,因为这是无效的SQL:

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;

如果您将分隔符更改为其他任何内容,MySQL 会识别完整的命令(从 CREATE FUNCTIONEND 并运行它。瞧!您的函数已创建。最后,当你运行你的函数,代码运行得很好因为函数体是由使用默认分隔符的多个语句组成的。

THEN SET v_parseStr = CONCAT(v_parseStr,v_char); END IF; SET i = i + 1; END WHILE; RETURN trim(v_parseStr); END $ DELIMITER ;

MySQL 命令行客户端(以及许多其他 SQL 客户端)默认分隔符是 ;。因此,当您键入原始代码时,MySQL 认为第一个命令在找到第一个 ; 的地方结束(在第 5 行,如错误消息所述),因此您会收到错误,因为这是无效的SQL:


如果您将分隔符更改为其他任何内容,MySQL 会识别完整的命令(从 CREATE FUNCTIONEND 并运行它。瞧!您的函数已创建。最后,当你运行你的函数,代码运行得很好因为函数体是由使用默认分隔符的多个语句组成的。

You have to change the delimiter so you can use ; inside the function:

DELIMITER $

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) )  DO
  SET v_char = SUBSTR(prm_strInput,i,1);
  IF v_char REGEXP '^[A-Za-z0-9]

In MySQL Command-Line Client (and many other SQL clients) the default delimiter is ;. So, when you type your original code, MySQL thinks the first command ends where the first ; is found (at line 5, as the error message states), thus you get an error because this is not valid SQL:

CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;

If you change the delimiter to anything else, MySQL identifies the complete command (from CREATE FUNCTION to END and runs it. Voilá! Your function is created. Finally, when you run your function, the code runs just fine because the function body is composed of several statements using the default delimiter.

THEN SET v_parseStr = CONCAT(v_parseStr,v_char); END IF; SET i = i + 1; END WHILE; RETURN trim(v_parseStr); END $ DELIMITER ;

In MySQL Command-Line Client (and many other SQL clients) the default delimiter is ;. So, when you type your original code, MySQL thinks the first command ends where the first ; is found (at line 5, as the error message states), thus you get an error because this is not valid SQL:


If you change the delimiter to anything else, MySQL identifies the complete command (from CREATE FUNCTION to END and runs it. Voilá! Your function is created. Finally, when you run your function, the code runs just fine because the function body is composed of several statements using the default delimiter.

无声静候 2024-11-25 22:04:24

我在此处找到了答案。

我发现这是一些奇怪的 DB Visualizer 问题。

将完整的块括在“--/”和“/”中对我有用:

--/
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) )  DO
  SET v_char = SUBSTR(prm_strInput,i,1);
  IF v_char REGEXP '^[A-Za-z0-9]
 THEN
        SET v_parseStr = CONCAT(v_parseStr,v_char);  
  END IF;
  SET i = i + 1;
END WHILE;
RETURN trim(v_parseStr);
END
/

I found the answer here.

I turns out it was some weird DB Visualizer issue.

Enclosing the complete block in "--/" and "/" worked for me:

--/
CREATE FUNCTION remove_non_alphanum (prm_strInput varchar(3000))
RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
  DECLARE i INT DEFAULT 1;
  DECLARE v_char VARCHAR(1);
  DECLARE v_parseStr VARCHAR(3000) DEFAULT '';
WHILE (i <= LENGTH(prm_strInput) )  DO
  SET v_char = SUBSTR(prm_strInput,i,1);
  IF v_char REGEXP '^[A-Za-z0-9]
 THEN
        SET v_parseStr = CONCAT(v_parseStr,v_char);  
  END IF;
  SET i = i + 1;
END WHILE;
RETURN trim(v_parseStr);
END
/
被你宠の有点坏 2024-11-25 22:04:24

替代方法

--/
CREATE FUNCTION ... 
/ 

是:

@delimiter $;
CREATE FUNCTION ...
@delimiter ;$

更多信息:http://www .dbvis.com/doc/main/doc/ug/sqlCommander/sqlCommander.html#mozTocId437790

An alternative to

--/
CREATE FUNCTION ... 
/ 

is:

@delimiter $;
CREATE FUNCTION ...
@delimiter ;$

More info: http://www.dbvis.com/doc/main/doc/ug/sqlCommander/sqlCommander.html#mozTocId437790

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