mysql事务批量处理祖籍列表名称

文章描述了一个名为`temp_sys_getOranizationName_001`的SQL存储过程,用于递归获取数据库表sys_organization中的部门及其层级结构。通过游标遍历和嵌套查询,确保了部门名称的完整路径。
摘要由CSDN通过智能技术生成
-- drop PROCEDURE temp_sys_getOranizationName_001;
create  PROCEDURE temp_sys_getOranizationName_001()
	BEGIN 
		DECLARE parentDeptId varchar(36) default '' ;  -- 父部门id
	    declare parentDeptName varchar(100) default '' ; -- 父部门名称
	    declare currentDeptName varchar(100) default '' ; -- 当前部门名称
	    declare retStr varchar(1000) default '' ;    --  返回字符串(部门1/部门1的子部门1/部门1的子部门2)
		declare deptName varchar(2000) default '' ;
	
		declare stopflag int default 0 ;
		declare tempUuid varchar(50) ;
		declare tempData varchar(100);
	
		declare oranizationData cursor for select uuid from sys_organization ; -- 创建一个游标变量,declare变量名cursor...
		declare continue handler for not found set stopflag = 1; -- 当变量中保存的结果都查询一遍(遍历),到达结尾,将变量stopflag设置为1,用于循环中判断是否结束
		open oranizationData ;-- 打开游标
		fetch oranizationData into tempUuid ;-- 游标向前走一步,取出一条记录放在变量 username 中
		while (stopflag = 0) do   -- 如果游标还没有结尾就继续
			begin 
				if tempUuid is not null and length(tempUuid) > 0 then
			        set currentDeptName = (select ifnull(organization_name, null) from sys_organization where uuid = tempUuid);
			    else
			        set retStr='';
			    end if;
					set tempData=tempUuid;
			    WHILE tempUuid is not null
			        do
			            SET parentDeptId = (SELECT parent_id FROM sys_organization WHERE uuid = tempUuid);
									
			            IF parentDeptId is not null THEN
			                set parentDeptName = (select organization_name from sys_organization td where td.uuid = parentDeptId);
			
			                if parentDeptName is not null then
			                    set deptName = concat(parentDeptName,'/', deptName);
			                end if;
			                SET tempUuid = parentDeptId;
			            ELSE
			                SET tempUuid = parentDeptId;
			            END IF;
			        END WHILE;
			
				update sys_organization set ancestors_names = concat(deptName,currentDeptName) where uuid = tempData ;

				
				fetch oranizationData into tempUuid;
				set deptName='';
				set parentDeptName='';
				set currentDeptName='';
				set parentDeptId='';
				
			end;
		end while;		-- 结束循环
		close oranizationData ;		-- 关闭游标
	END
	

call temp_sys_getOranizationName_001



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值