今天遇到了不能创建mysql函数

今天用navicat 不能创建函数,查询了

MySQL函数不能创建,是未开启功能:

  1. mysql> show variables like '%func%';  
  2. +---------------------------------+-------+  
  3. | Variable_name                   | Value |  
  4. +---------------------------------+-------+  
  5. | log_bin_trust_function_creators | OFF   |  
  6. +---------------------------------+-------+  
  7. 1 row in set (0.00 sec)  
  8.  
  9. mysql> set global log_bin_trust_function_creators=1;  
  10. Query OK, 0 rows affected (0.00 sec)  
  11.  
  12. mysql> show variables like '%func%';  
  13. +---------------------------------+-------+  
  14. | Variable_name                   | Value |  
  15. +---------------------------------+-------+  
  16. | log_bin_trust_function_creators | ON    |  
  17. +---------------------------------+-------+  
  18. 1 row in set (0.00 sec)mysql> 

==================

这里写了2个函数,功能是去除用逗号分隔的字符串中重复的字符

CREATE DEFINER=`root`@`localhost` FUNCTION `str_str_unique`(str text,str1 text) RETURNS text CHARSET utf8
BEGIN
#str 要处理的字符 str1 分隔符
#功能:处理字符串中重复的字符
  declare stri TEXT;
  declare i int DEFAULT 1;
  declare len int DEFAULT 0;
  declare tmp TEXT default '' ;
  declare tmp_str varchar(255) default '' ;
  declare site int DEFAULT 0;
  set len = str_str_count(str,str1);
  repeat
    set tmp_str=SUBSTRING_INDEX(SUBSTRING_INDEX(str,str1,i),str1,-1);
    set site = FIND_IN_SET(tmp_str,tmp);
    if(site=0 and tmp_str is not null and tmp_str !='') then
      if(length(tmp)>0)then
        set tmp = concat(tmp,str1,tmp_str);
      else
        set tmp = concat(tmp,tmp_str);
      end if;
    end if;
    set i = i + 1;
  until i >len+2 end repeat;
  set stri = tmp;
  return stri;
END

CREATE DEFINER=`root`@`%` FUNCTION `str_str_count`(str varchar(255),stri varchar(20) ) RETURNS int(12)
BEGIN

  # 测量字符串中分隔符的个数
  DECLARE total int(20);
  SELECT LENGTH(str) - LENGTH(REPLACE(str, stri, '')) into total;
  RETURN total;
END

猜你喜欢

转载自www.cnblogs.com/xmnote/p/12066115.html