Heim  >  Artikel  >  Datenbank  >  浅谈mysql 自定义函数_MySQL

浅谈mysql 自定义函数_MySQL

WBOY
WBOYOriginal
2016-06-01 13:05:51780Durchsuche

因为工作需要,要写一个mysql的自定义行数,如下

DELIMITER $$
DROP FUNCTION IF EXISTS `onlineFunction`$$
CREATE FUNCTION `onlineFunction`(rrrr VARCHAR(50)) RETURNS VARCHAR(255)
BEGIN
IF(rrrr='online') THEN RETURN '上线';END IF;
END$$
DELIMITER ;

第一行DELIMITER 定义一个结束标识符,因为MySQL默认是以分号作为SQL语句的结束符的,而函数体内部要用到分号,所以会跟默认的SQL结束符发生冲突,所以需要先定义一个其他的符号作为SQL的结束符。没有加这个定义的话...

错误码: 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 'end' at line 1

第二行是删除同名的类,不然会...

错误码: 1304
FUNCTION onlineFunction already exists

第三行第一函数名,函数变量,和返回类型

第四行begin是起始,与end$$对应

第五行是if判断语句,格式为

if(...) then
....;
elseif
....;
else
.....;
end if;
return ..;

有时候mysql不能建立自定义函数是因为该功能2未开启

输入 show variables like '%func%'; 命令

会看到 log_bin_trust_function_creators 的状态,如果是OFF表示自定义函数功能是关闭的

输入命令 set global log_bin_trust_function_creators=1;

可将 log_bin_trust_function_creators 开启自定义函数功能

但是这样设置是一个临时的方案,因为mysql自动重启后状态又会变为OFF,所以需要在

在服务启动时加上 “--log-bin-trust-function-creators=1 ”参数。
或在my.ini(my.cnf)中的[mysqld]区段中加上 log-bin-trust-function-creators=1。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn