MySQL创建Function时报错误码1418的解决方案

来源:互联网 发布:三端口环行器 编辑:程序博客网 时间:2024/05/29 03:37

最近在创建function时,系统提示以下错误:
这里写图片描述

This function has none of DETERMINISTIC, NO SQL,
or READS SQL DATA in its declaration and binary logging is enabled(you might want to use the less safe log_bin_trust_function_creators
variable)

查阅相关资料,意思是说binlog启用时,创建的函数没有声明类型,因为binlog在主从复制需要知道这个函数创建语句是什么类型,否则同步数据会有不一致现象。

mysql开启了bin-log, 我们就必须指定我们的函数是否是哪种类型:
1 DETERMINISTIC 不确定的
2 NO SQL 没有SQl语句,当然也不会修改数据
3 READS SQL DATA 只是读取数据,当然也不会修改数据
4 MODIFIES SQL DATA 要修改数据
5 CONTAINS SQL 包含了SQL语句

根据上面的提示,我增加了函数类型,以root用户权限创建好了!
附改进后的代码:

CREATE DEFINER=`xxx`@`%` FUNCTION `FN_TOTAL_SYL`(  fundid_ int(11), /**资金账户id*/  beginDay int(11), /**开始日*/  endDay int(11) /**结束日*/) RETURNS decimal(12,4)    READS SQL DATABEGIN  /**从结束日开始计算总的收益率:根据日收益率复利计算*/  declare totalSyl decimal(12,4) default 1.0;  declare daySyl decimal(12,4);  declare done int default -1;    /* 声明游标 */      declare myCursor cursor for select  REAL_DAY_SYL from tpm_his_fund where abs(REAL_DAY_SYL)<11.0 and day between beginDay and endDay and fundid = fundid_;      /* 当游标到达尾部时,mysql自动设置done=1 */         declare continue handler for not found set done=1;      /* 打开游标 */      open myCursor;      /* 循环开始 */      myLoop: LOOP          /* 移动游标并赋值 */          fetch myCursor into daySyl;          if done = 1 then              leave myLoop;          end if;        set totalSyl = totalSyl * ( 1 + ifnull(daySyl, 0) / 100 );    /* 循环结束 */      end loop myLoop;    /* 关闭游标 */    close myCursor;      /**返回值*/  RETURN round((totalSyl - 1) * 100, 4);END;

注意:改进部分就是在Begin之前,returns之后指定了类型:READS SQL DATA

另外还有一种办法就是直接设置变量:
1. mysql> SET GLOBAL log_bin_trust_function_creators = 1;
2. 系统启动时 –log-bin-trust-function-creators=1
3. 在my.ini(linux下为my.conf)文件中 [mysqld] 标记后加一行内容为 log_bin_trust_routine_creators=1;

建议采用第一种方法,方便快捷,但是重启后失效。

最好还是创建函数时指定类型,这样是个好的习惯!

0 0
原创粉丝点击