因為工作需要,要寫一個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語句的結束符的,而mysql體內部要用到分號,所以會跟默認的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
第二行是mysql同名的類,不然會…
錯誤碼: 1304
function onlinefunction already exists
第三行第一函數名,函數mysql,和返回類型
第四行begin是起始,與end$$對應
第五行是ifmysql,格式為
if(…) then
….;
elseif
….;
else
…..;
end if;
return ..;
有時候mysql不能建立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。