1

我正在尝试使用我发现的 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
4

1 回答 1

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)
于 2017-07-14T17:50:39.090 回答