首页 > 数据库 >MySQL存储过程、递归调用

MySQL存储过程、递归调用

时间:2023-10-10 14:22:25浏览次数:46  
标签:调用 递归 -- system code dict MySQL variable type

MySQL存储过程、递归调用

实现字典数据的预处理,维护类别表、数据表、tree表,数据库在jwzh_manager库

1、先将excel导入到system_dict表,按表字段注释对应匹配。

2、编写存储过程处理数据

CREATE DEFINER=`root`@`%` PROCEDURE `handle_system_dict`()
BEGIN
	#Routine body goes here...
	-- 声明变量
	declare done int default false;
	declare dict_type_variable varchar(100);
	
	-- 创建一个 name 为cur的游标
	declare cur cursor for select dict_type from system_dict group by dict_name, dict_type having count(dict_parent_code) >0;
	
	-- 当游标走到末尾 也就是遍历完成之后 将标志位设置成true
	declare continue HANDLER for not found set done = true;
	
	-- 删除处理表数据
	truncate table system_dict_type_bak;
	truncate table system_dict_data_bak;
	truncate table system_dict_tree_bak;
	
	-- 插入type表
	insert into system_dict_type_bak (dict_name, dict_type, dict_view)
	select dict_name, dict_type, '1' from system_dict group by dict_name, dict_type having count(dict_parent_code) =0
	union all
	select dict_name, dict_type, '2' from system_dict group by dict_name, dict_type having count(dict_parent_code) >0;
	commit;
	
	-- 插入data表
	insert into system_dict_data_bak (dict_sort, dict_label, dict_code, dict_type)
	select dict_sort, dict_label, dict_code, dict_type from system_dict where dict_type in ( select dict_type from system_dict group by dict_name, dict_type having count(dict_parent_code) =0 );
	commit;

	-- 插入tree表
	insert into system_dict_tree_bak (dict_code, dict_parent_code, dict_sort, dict_label_full, dict_type)
	select dict_code, dict_parent_code, dict_sort, dict_label, dict_type from system_dict where dict_type in ( select dict_type from system_dict group by dict_name, dict_type having count(dict_parent_code) >0 );
	commit;
	
	-- 开启游标
	open cur;
	
		-- 进行循环
		read_loop:loop
		
			-- 将游标中是数据填充到 dict_type 当中
			fetch cur into dict_type_variable;
			
			if done then -- 如果到达末尾
				leave read_loop; -- 跳出循环相当于 break;
			end if;
						
			-- 逻辑操作
			CALL handle_system_dict_tree_first(dict_type_variable); 
		
		-- 结束循环
		end loop;
	
	-- 关闭游标
	close cur;
	
	update system_dict_tree_bak set ancestors = substring(ancestors, 2);
	commit;
	
END
CREATE DEFINER=`root`@`%` PROCEDURE `handle_system_dict_tree_first`(IN `dictType` varchar(100))
BEGIN
	#Routine body goes here...
	
	-- 声明变量
	declare done int default false;
	declare ancestors_variable varchar(500); 
	declare dict_type_variable varchar(100);  
	declare dict_level int;
	declare dict_code_variable varchar(32);
	
	-- 如果type为1获取第一级数据
	declare cur cursor for select ancestors, dict_type, dict_code from system_dict_tree_bak where dict_parent_code is null and dict_type = dictType;
	
	-- 当游标走到末尾 也就是遍历完成之后 将标志位设置成true
	declare continue HANDLER for not found set done = true;
	
	-- ancestors, dict_level参数赋值
	set dict_level = 0;
	
	-- 修改第一级 dict_level
	update system_dict_tree_bak set dict_level = CONCAT(dict_level, '') where dict_parent_code is null and dict_type = dictType;
	commit;
	
	-- 开启游标
	open cur;

		-- 进行循环
		read_loop:loop

			-- 将游标中是数据填充到 dict_type 当中
			fetch cur into ancestors_variable, dict_type_variable, dict_code_variable;

			if done then -- 如果到达末尾
				leave read_loop; -- 跳出循环相当于 break;
			end if;
			
-- 			下面的select是输出断点
-- 			select dict_level, ancestors_variable, dict_type_variable, dict_code_variable;

			-- 逻辑操作
			CALL handle_system_dict_tree_other(dict_type_variable, dict_code_variable, ancestors_variable, dict_level);

		-- 结束循环
		end loop;

	-- 关闭游标
	close cur;

END
CREATE DEFINER=`root`@`%` PROCEDURE `handle_system_dict_tree_other`(IN `dictType` varchar(100),IN `dictCode` varchar(32),IN `dictAncestors` varchar(500), IN `dictLevel` int)
BEGIN
	#Routine body goes here...
	
	-- 声明变量
	declare done int default false;
	declare ancestors_variable varchar(500); 
	declare dict_type_variable varchar(100);  
	declare dict_level int;
	declare dict_code_variable varchar(32);
	

	-- 获取下一级
	declare cur cursor for select ancestors, dict_type, dict_code from system_dict_tree_bak where dict_parent_code = dictCode and dict_type = dictType;

	-- 当游标走到末尾 也就是遍历完成之后 将标志位设置成true
	declare continue HANDLER for not found set done = true;
	
	-- ancestors, dict_level参数赋值
	set ancestors_variable = CONCAT(dictAncestors, ',', dictCode);
	set dict_level = dictLevel + 1;
	
-- 	设置递归层级
	SET max_sp_recursion_depth=10;
	-- 修改下一级 ancestors, dict_level
	update system_dict_tree_bak set ancestors = ancestors_variable, dict_level = CONCAT(dict_level, '') where dict_parent_code = dictCode and dict_type = dictType;
	commit;
	
-- 			下面的select是输出断点
-- 			select dict_level, ancestors_variable, dict_type_variable, dict_code_variable;

	-- 开启游标
	open cur;

		-- 进行循环
		read_loop:loop

			-- 将游标中是数据填充到 dict_type 当中
			fetch cur into ancestors_variable, dict_type_variable, dict_code_variable;

			if done then -- 如果到达末尾
				leave read_loop; -- 跳出循环相当于 break;
			end if;

			-- 逻辑操作
			CALL handle_system_dict_tree_other(dict_type_variable, dict_code_variable, ancestors_variable, dict_level);

		-- 结束循环
		end loop;

	-- 关闭游标
	close cur;

END

3、执行存储过程,结束后自行验证数据准确性,没有异议后分别插入正式表

标签:调用,递归,--,system,code,dict,MySQL,variable,type
From: https://www.cnblogs.com/LeeJ0/p/17754563.html

相关文章

  • 9月《中国数据库行业分析报告》已发布,47页干货带你详览 MySQL 崛起之路!
    为了帮助大家及时了解中国数据库行业发展现状、梳理当前数据库市场环境和产品生态等情况,从2022年4月起,墨天轮社区行业分析研究团队出品将持续每月为大家推出最新《中国数据库行业分析报告》,持续传播数据技术知识、努力促进技术创新与行业生态发展,目前已更至第十七期,并发布了共计1......
  • MySQL进阶篇:第三章_SQL性能分析
    MySQL进阶篇:第三章_SQL性能分析SQL执行频率MySQL客户端连接成功后,通过show[session|global]status命令可以提供服务器状态信息。通过如下指令,可以查看当前数据库的INSERT、UPDATE、DELETE、SELECT的访问频次:--session是查看当前会话;--global是查询全局数据;SHOW......
  • 定时备份mysql脚本
    定时备份mysql指定数据库脚本,保留60天#!/bin/bash#pathcd/opt/pmo/mysql_datatarget_directory="/opt/pmo/mysql_data"#gettimenowcurrent_time=$(date+%s)#cal22monthsagobefore_time=$(date-d"60daysago"+%s)file_name=metersphere_`date+......
  • MySQL进阶篇:第四章_四.一_ 索引使用_最左前缀法则
    索引使用_最左前缀法则最左前缀法则如果索引了多列(联合索引),要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列。如果跳跃某一列,索引将会部分失效(后面的字段索引失效)。以tb_user表为例,我们先来查看一下之前tb_user表所创建的索引。在......
  • 《MySQL与MariaDB学习指南》高清高质量 原版电子书PDF+源码
    下载:https://pan.quark.cn/s/2392eb287424......
  • 多个接口同时调用同一个方法
    如果多个接口同时调用同一个方法,会增加该方法的负载和并发量。这可能会导致性能问题,特别是当方法需要执行大量计算或涉及到I/O操作时。为了避免性能问题,可以采取以下措施:缓存结果:对于一些计算结果比较稳定的方法,可以将结果缓存起来,避免重复计算。异步执行:对于一些需要进行I......
  • mysql 创建表分区 list分区
    CREATETABLE`goods`(`id`varchar(36)NOTNULL,`goods_name`varchar(256)NOTNULLCOMMENT'产品名称',`release_version`varchar(8)NOTNULL,PRIMARYKEY(`release_version`,`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4ROW_FORMAT=DYNAMIC;al......
  • Feign调用异步方式
    Feign是一个基于HTTP的客户端,它使用了Java的注解来简化HTTPAPI的开发。在Feign中,异步调用可以通过使用Java的CompletableFuture来实现。CompletableFuture是Java8中引入的一个异步编程工具,它可以让开发者以更加简洁的方式编写异步代码。在Feign中,异步调用需要使用@Async注解来......
  • Feign调用重试
    Feign是一个JavaHTTP客户端库,它简化了使用HTTPAPI的过程。当使用Feign调用远程API时,有时会遇到连接超时或请求失败的情况。为了解决这些问题,可以使用Feign的重试功能。Feign的重试功能可以通过以下步骤来实现:添加Feign的retry依赖在pom.xml文件中添加以下依赖:<dependency>......
  • python接口自动化之request请求,如何使用 Python调用 API?
    Python实战|如何使用Python调用API一、HTTP 请求HTTP 请求是在 HTTP 协议下的一种数据格式,用于向服务器发送请求,其通常由请求行、请求头和请求体三部分构成,请求头和请求体之间用空行隔开,其中各部分包含的信息如下:请求行 (Request Line):包括请求方法 (GET请求、POST请......