mysql自定义函数获取上级的路径实例
sql
CREATE FUNCTION `querydept_path_name`(depetId VARCHAR(255)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE sTempPath VARCHAR(255);
DECLARE sTempChd VARCHAR(255);
DECLARE sFullPath VARCHAR(255);
SET sTempChd = depetId;
SET sFullPath ='';
SET sTempPath ='';
WHILE (sTempChd <> '' ) DO
select ifnull((SELECT dept_name FROM dept_info WHERE id = sTempChd),'') INTO sTempPath;
SET sFullPath = CONCAT(sFullPath,sTempPath,'/');
select ifnull((SELECT parent_id FROM dept_info WHERE id = sTempChd),'') INTO sTempChd;
END WHILE;
RETURN sFullPath;
END
函数创建失败错误信息
创建函数时报错信息
sql
[Err] 1418 - 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)
解决办法
解决方法
- 在mysql数据库中执行以下语句 (临时生效,重启后失效)
sql
set global log_bin_trust_function_creators=TRUE;
或者
sql
set global log_bin_trust_function_creators=1;
- 在配置文件
/etc/my.cnf
的[mysqld]
或者my-default.ini
文件中配置
sql
[mysqld]
log_bin_trust_function_creators=1
测试函数路径
默认路径为逆序
sql
select querydept_path_name('10011')
路径正序
sql
select REVERSE(querydept_path_name('10011'))