簡(jiǎn)單介紹mysql自定義函數(shù)

因?yàn)楣ぷ餍枰獙?xiě)一個(gè)mysql的自定義行數(shù),如下
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 定義一個(gè)結(jié)束標(biāo)識(shí)符,因?yàn)閙ysql默認(rèn)是以分號(hào)作為sql語(yǔ)句的結(jié)束符的,而mysql體內(nèi)部要用到分號(hào),所以會(huì)跟默認(rèn)的sql結(jié)束符發(fā)生沖突,所以需要先定義一個(gè)其他的符號(hào)作為sql的結(jié)束符。沒(méi)有加這個(gè)定義的話…
錯(cuò)誤碼: 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

第二行是mysql同名的類,不然會(huì)…
錯(cuò)誤碼: 1304
function onlinefunction already exists

第三行第一函數(shù)名,函數(shù)mysql,和返回類型

第四行begin是起始,與end$$對(duì)應(yīng)

第五行是ifmysql,格式為
if(…) then
….;
elseif
….;
else
…..;
end if;
return ..;
有時(shí)候mysql不能建立mysql是因?yàn)樵摴δ?未開(kāi)啟

輸入 show variables like ‘%func%’; 命令

會(huì)看到 log_bin_trust_function_creators 的狀態(tài),如果是off表示自定義函數(shù)功能是關(guān)閉的

輸入命令 set global log_bin_trust_function_creators=1;

可將 log_bin_trust_function_creators 開(kāi)啟自定義函數(shù)功能

但是這樣設(shè)置是一個(gè)臨時(shí)的方案,因?yàn)閙ysql自動(dòng)重啟后狀態(tài)又會(huì)變?yōu)閛ff,所以需要在

在服務(wù)啟動(dòng)時(shí)加上 “–log-bin-trust-function-creators=1 ”參數(shù)。

或在my.ini(my.cnf)中的[mysqld]區(qū)段中加上 log-bin-trust-function-creators=1。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊14 分享