MySQL - 存储过程游标循环插入数据

1. 数据库表:

CREATE TABLE `t_role_policy` (
  `id` varchar(32) NOT NULL,
  `roleId` varchar(32) NOT NULL COMMENT '外键,角色ID',
  `policyId` varchar(32) NOT NULL COMMENT '外键,策略ID',
  `relationship` tinyint(4) NOT NULL COMMENT '权限关系:拥有关系、可授予关系',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='角色-权限策略关联表';

2. 存储过程游标循环插入数据:

# 如果存在名字为handle_data的procedure则删除
drop procedure if exists handle_data; 
# 定义//为一句sql的结束标志,取消;的所代表的意义
delimiter // 
# 声明存储过程handle_data()
CREATE PROCEDURE handle_data ( ) 
  # 存储过程开始
  BEGIN
	# 定义一个变量,while循环时使用,存储roleId
	DECLARE u_roleId varchar( 32 );
	# 定义一个变量,while循环时使用,存储policyI
	DECLARE u_policyId varchar( 32 );
	# 为下面while循环建立一个退出标志,
    DECLARE flag INT DEFAULT 0;
	# 定义一个游标来记录sql查询的结果
    DECLARE deviceCodeList CURSOR FOR ( select  `roleId`, `policyId` from t_role_policy where policyId in('fx-policy-fec8332fd0dc12f0000020',
		'fx-policy-fec8332fd0dc12f000000b','fx-policy-fec8332fd0dc12f000002f','fx-policy-fec8332fd0dc12f0000014'));
    # 当游标遍历完后将flag的值设置为1
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag = 1;
	# 打开游标
    OPEN deviceCodeList;
	# 将游标中的值赋给定义好的变量,实现for循环的要点
    FETCH deviceCodeList INTO u_roleId,u_policyId;
	# 当flag不等于1时,会一直循环
    WHILE flag != 1 DO
		# 执行需要循环处理的SQL
		if(u_policyId='fx-policy-fec8332fd0dc12f0000020') then
			INSERT INTO `t_role_policy` (`id`, `roleId`, `policyId`, `relationship`) VALUES
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000021', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66ad0e415c8', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c30000001', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000005', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000003', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c9000000b', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000009', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000007', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66ca000000d', 0);
		elseif(u_policyId='fx-policy-fec8332fd0dc12f000000b') then
			INSERT INTO `t_role_policy` (`id`, `roleId`, `policyId`, `relationship`) VALUES 
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66cb000000e', 0),
	    (concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000022', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c9000000c', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c9000000a', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000008', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000006', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c50000004', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66c40000002', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66b10000000', 0);
		elseif(u_policyId='fx-policy-fec8332fd0dc12f000002f') then
			INSERT INTO `t_role_policy` (`id`, `roleId`, `policyId`, `relationship`) VALUES 
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66cb000000f', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000017', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d70000013', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000019', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000015', 0);
		elseif(u_policyId='fx-policy-fec8332fd0dc12f0000014') then
			INSERT INTO `t_role_policy` (`id`, `roleId`, `policyId`, `relationship`) VALUES 
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000020', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000018', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d80000014', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66cd0000012', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66cc0000010', 0),
			(concat('fx-rolepo-',substring(md5(rand()), 1, 22)), u_roleId, 'fx-policy-fec84451f3b66d90000016', 0);
		end if;
	  # 游标往后移,注:不能漏
      FETCH deviceCodeList INTO u_roleId,u_policyId;
    end while;
	 # 关闭游标
    CLOSE deviceCodeList;
	# 存储过程结束 
  end //
    
delimiter ;
# 调用存储过程
call handle_data();

3. 测试:

# 添加一行数据
INSERT INTO `t_role_policy` (`id`, `roleId`, `policyId`, `relationship`) VALUES ('fx-rolepo-b8c7ef290cc57f1412bdb0', 'fx-role-fec8339100dc0d10000003', 'fx-policy-fec8332fd0dc12f0000020', 0);

# 调用存储过程
call handle_data(); 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一直在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值