日期:2014-05-16  浏览次数:20732 次

MYSQL FUNCTION的问题
我想自己创建function,怎么都执行不了。原来在ORACLE下写的怎么改都不行。请各位高手能给我一个能执行成功的例子,让我学习一下基本写法。谢谢了~~


后来我从百度上搜索了一个中文手册

create function hello return varchar(50) 
return concat('Hello!');

会报如下错误。

Error Code : 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 near 'return varchar(50) 
return concat('Hello!')' at line 1
(0 ms taken)

------解决方案--------------------


mySQL版本5以上。
以下摘自 MySQL 5.1 Reference Manual

SQL code
mysql> CREATE FUNCTION hello (s CHAR(20)) RETURNS CHAR(50)
    -> RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world!  |
+----------------+
1 row in set (0.00 sec)

------解决方案--------------------
完整的


SQL code


set global log_bin_trust_function_creators = 1;
DELIMITER $$

DROP FUNCTION IF EXISTS `hello`$$

CREATE DEFINER=`root`@`%` FUNCTION `hello`() RETURNS varchar(50) CHARSET utf8
begin
return concat('Hello!');
end$$

DELIMITER ;