Mysql错误分隔符:行分隔符出错

时间:2017-07-14 15:10:12

标签: mysql mariadb mysql-routines

我正在尝试使用我找到的mysql函数,但我似乎遇到了一些错误。这是功能:

SET GLOBAL log_bin_trust_function_creators=1; 
DROP FUNCTION IF EXISTS digits; 
DELIMITER $$
CREATE FUNCTION digits( str CHAR(32) ) RETURNS CHAR(32) 
BEGIN 
  DECLARE i, len SMALLINT DEFAULT 1; 
  DECLARE ret CHAR(32) DEFAULT ''; 
  DECLARE c CHAR(1); 
  SET len = CHAR_LENGTH( str ); 
  REPEAT 
    BEGIN 
      SET c = MID( str, i, 1 ); 
      IF c BETWEEN '0' AND '9' THEN  
        SET ret=CONCAT(ret,c); 
      END IF; 
      SET i = i + 1; 
    END ; 
  UNTIL i > len END REPEAT; 
  RETURN ret; 
END 
$$
DELIMITER ; 

SELECT digits('123ab45cde6789fg');

我正在使用Mariadb 5.5

Server version: 5.5.50-MariaDB MariaDB Server

以下是我看到的错误:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '$$
DELIMITER ; 

我尝试了一些建议,例如更改最初是管道的分隔符。然而,将分隔符从END旁边移动到我已经完成的下面,但两者中似乎都发生了错误。

我已经尝试过修改错误的基本修改,但它仍然会发生,似乎在删除此部分时我不再收到任何错误,但结果不正确。

REPEAT 
  BEGIN 
    SET c = MID( str, i, 1 ); 
    IF c BETWEEN '0' AND '9' THEN  
      SET ret=CONCAT(ret,c); 
    END IF; 
    SET i = i + 1; 
  END ; 
UNTIL i > len END REPEAT; 

任何指针都会很棒。

更新

我测试过其他一些版本并遇到同样的问题。我目前正在使用Navicat。

Server version: 5.5.50-MariaDB MariaDB Server
Server version: 5.5.31-0ubuntu0.12.10.1 (Ubuntu)
Server version: 5.1.73 Source distribution
Server version: 5.5.52-MariaDB MariaDB Server

1 个答案:

答案 0 :(得分:0)

我无法重现这个问题:

MariaDB [_]> SELECT VERSION();
+----------------+
| VERSION()      |
+----------------+
| 5.5.56-MariaDB |
+----------------+
1 row in set (0.00 sec)

MariaDB [_]> SET GLOBAL log_bin_trust_function_creators=1;
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> DROP FUNCTION IF EXISTS digits;
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> DELIMITER $$

MariaDB [_]> CREATE FUNCTION digits( str CHAR(32) )
    -> RETURNS CHAR(32)
    -> BEGIN
    ->   DECLARE i, len SMALLINT DEFAULT 1;
    ->   DECLARE ret CHAR(32) DEFAULT '';
    ->   DECLARE c CHAR(1);
    ->   SET len = CHAR_LENGTH( str );
    ->   REPEAT
    ->     BEGIN
    ->       SET c = MID( str, i, 1 );
    ->       IF c BETWEEN '0' AND '9' THEN
    ->         SET ret=CONCAT(ret,c);
    ->       END IF;
    ->       SET i = i + 1;
    ->     END;
    ->   UNTIL i > len END REPEAT;
    ->   RETURN ret;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

MariaDB [_]> DELIMITER ; 

MariaDB [_]> SELECT digits('123ab45cde6789fg');
+----------------------------+
| digits('123ab45cde6789fg') |
+----------------------------+
| 123456789                  |
+----------------------------+
1 row in set (0.00 sec)