我想自己创建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)
后来我从百度上搜索了一个中文手册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.1 Reference Manualmysql> 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)
[align=center]==== 思想重于技巧 ====
[/align]
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 ;
SET GLOBAL log_bin_trust_function_creators = 1;
begin
DECLARE c INT(10) default 0;
set c=a+b;
return c;
end定义返回类型是 returns , 而不是 return 。