【发布时间】:2014-02-01 22:02:17
【问题描述】:
如您所知,MariaDB 的小版本 (5.5-) 据说与 MySQL 兼容(我发现了缺陷,但他们说它是 90%)。我在我们的环境中使用两者,AWS RDS 上的 MySQL 和我们本地开发服务器和开发盒上的 Maria。我目前正在尝试组合一个函数,该函数将使用随机数据自动填充一行
表结构会很简单,像这样
DROP DATABASE IF EXISTS `foodb`;
CREATE DATABASE `foodb`;
USE `foodb`;
DROP TABLE IF EXISTS `footable`;
CREATE TABLE `footable` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`garbage` varchar(128) DEFAULT garbageString(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
函数garbageString() 是这样的
DROP FUNCTION IF EXISTS garbageString;
CREATE FUNCTION garbageString($length int)
RETURNS varchar(128)
BEGIN
SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
SET @charLen = length(@chars);
SET @randomString = '';
WHILE length(@randomString) < $length DO
SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
END WHILE;
RETURN @randomString ;
END;
虽然 MySQL 和 Maria 都试图让这个函数运行,但我很头疼,这是来自 MY 的错误转储
MySQL [foodatabase]> CREATE FUNCTION garbageString($length int)
-> RETURNS varchar(128)
-> BEGIN
->
-> SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
ERROR 1064 (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
MySQL [foodatabase]> SET @charLen = length(@chars);
Query OK, 0 rows affected (0.03 sec)
MySQL [foodatabase]>
MySQL [foodatabase]> SET @randomString = '';
Query OK, 0 rows affected (0.03 sec)
MySQL [foodatabase]>
MySQL [foodatabase]> WHILE length(@randomString) < $length DO
-> SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
ERROR 1064 (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 'WHILE length(@randomString) < $length DO
SET @randomString = concat(@rando' at line 1
MySQL [foodatabase]> END WHILE;
ERROR 1064 (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 'END WHILE' at line 1
MySQL [foodatabase]>
MySQL [foodatabase]> RETURN @randomString ;
ERROR 1064 (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 'RETURN @randomString' at line 1
MySQL [foodatabase]> END;
这是玛丽亚的一个
MariaDB [foodatabase]> CREATE FUNCTION garbageString($length int)
-> RETURNS varchar(128)
-> BEGIN
->
-> SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
ERROR 1064 (42000): 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 '' at line 5
MariaDB [foodatabase]> SET @charLen = length(@chars);
Query OK, 0 rows affected (0.00 sec)
MariaDB [foodatabase]>
MariaDB [foodatabase]> SET @randomString = '';
Query OK, 0 rows affected (0.00 sec)
MariaDB [foodatabase]>
MariaDB [foodatabase]> WHILE length(@randomString) < $length DO
-> SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
ERROR 1064 (42000): 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 'WHILE length(@randomString) < $length DO
SET @randomString = concat(@rando' at line 1
MariaDB [foodatabase]> END WHILE;
ERROR 1064 (42000): 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 'END WHILE' at line 1
MariaDB [foodatabase]>
MariaDB [foodatabase]> RETURN @randomString ;
ERROR 1064 (42000): 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 'RETURN @randomString' at line 1
MariaDB [foodatabase]> END;
有什么方法可以让这个函数工作,并让它在两种数据库类型上工作?
看起来他们都在同一个地方窒息,我只是不想有任何意外
【问题讨论】:
标签: mysql user-defined-functions mariadb