123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- -- 2019年1月17日11:19:45
- -- 项目整合规则:
- -- 超级前台部分,保留id小的,删除id大的
- -- 超级前台--服务之窗部分,保留服务之窗,删除超级前台
- -- 1.编写存储过程,对两个项目合并
- -- 将语句的结束符号从分号;临时改为两个$$(可以是自定义)
- delimiter $$
- -- 存储过程(项目数据整合)
- drop procedure if exists sync_org;
- -- 定义存储过程
- -- reserve_orgId:保留的项目id del_orgId:删除的项目id
- CREATE PROCEDURE sync_org(in reserve_orgId INT,in del_orgId INT)
- BEGIN
- -- 创建接收游标数据的变量
- declare table_name VARCHAR(50);
- declare column_name varchar(50);
- -- 循环结束标识,为true时结束循环
- DECLARE done int DEFAULT FALSE;
- -- 创建游标
- DECLARE cur CURSOR FOR
- SELECT
- t1.TABLE_NAME,
- t1.COLUMN_NAME
- FROM
- information_schema.`COLUMNS` t1
- LEFT JOIN information_schema.`TABLES` t2 ON t1.TABLE_NAME = t2.TABLE_NAME
- WHERE
- t1.COLUMN_NAME LIKE 'org%'
- AND t2.TABLE_ROWS > 0
- GROUP BY
- t1.TABLE_NAME;
- -- 指定游标循环结束时的标识 done改为true
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
- -- 打开游标
- OPEN cur;
- -- 开始循环游标数据
- REPEAT
- FETCH cur INTO table_name,column_name;
- -- 声明sql执行模板
- SET @preSql =CONCAT('UPDATE ',table_name,' SET ',column_name,'=',reserve_orgId,' WHERE ',column_name,' = ',del_orgId);
- PREPARE stmt FROM @preSql;
- if not done THEN
- -- 需要执行的操作
- -- UPDATE table_name SET column_name=reserve_orgId WHERE column_name=del_orgId;
- EXECUTE stmt;
- DEALLOCATE PREPARE stmt;
- END IF;
- until done end repeat;
- -- 关闭游标
- CLOSE cur;
- -- 结束过程
- END $$
- -- 将语句的结束符号恢复为分号
- delimiter ;
- -- 2.循环调用存储过程:sync_org(),对所有需要同步的项目进行同步
- -- 创建临时表存储项目整合的id
- DROP TABLE IF EXISTS `tb_sync_org`;
- CREATE TABLE `tb_sync_org` (
- `id` BIGINT (20) NOT NULL AUTO_INCREMENT,
- `reverse_id` INT (11) DEFAULT NULL COMMENT '保留的项目id',
- `del_id` INT (11) DEFAULT NULL COMMENT '删除的项目id',
- PRIMARY KEY (`id`)
- ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '项目整合临时表';
- -- 插入数据 超级前台数据整合部分
- INSERT INTO `tb_sync_org` (`reverse_id`, `del_id`)
- VALUES
- (55, 511),
- (101, 116),
- (171, 412),
- (38, 39),
- (38, 397),
- (336, 475),
- (103, 351),
- (107, 349),
- (36, 42),
- (114, 348),
- (363, 391),
- (498, 534),
- (394, 500);
- -- 正式数据库插入该数据;测试数据库注释掉;
- -- 插入数据 超级前台--服务之窗数据整合部分
- -- INSERT INTO `tb_sync_org` (`reverse_id`, `del_id`)
- -- VALUES
- -- (616, 11),
- -- (619, 362),
- -- (626, 6),
- -- (633, 288),
- -- (641, 421),
- -- (646, 390),
- -- (647, 370),
- -- (650, 195);
- -- 新建存储过程:循环整合项目数据
- -- 声明结束标识符为 $$
- delimiter $$
- -- 新建存储过程
- DROP PROCEDURE IF EXISTS begin_sync_org;
- CREATE PROCEDURE begin_sync_org()
- BEGIN
- -- 定义变量接收游标中保存的项目id
- DECLARE reserve_id INT;
- -- 定义变量接收游标中删除的项目id
- DECLARE del_id INT;
- -- 声明循环结束标识符
- DECLARE done int DEFAULT FALSE;
- -- 定义游标
- DECLARE cur CURSOR FOR SELECT t1.reverse_id,t1.del_id FROM tb_sync_org t1;
- -- 指定游标循环结束时的标识 done改为true
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=TRUE;
- -- 打开游标
- OPEN cur;
- -- 开始循环
- REPEAT
- FETCH cur INTO reserve_id,del_id;
- if not done THEN
- -- 需要执行的操作
- CALL sync_org(reserve_id,del_id);
- END IF;
- until done end repeat;
- -- 关闭游标
- CLOSE cur;
- -- 结束过程
- END $$
- -- 声明结束标识符为 ;
- delimiter ;
- -- 调用存储过程进行同步
- -- CALL begin_sync_org();
- -- 删除临时表
- -- DROP TABLE IF EXISTS `tb_sync_org`;
- -- 删除被合并的项目
- -- UPDATE tb_organization SET isEnabled=0 WHERE ID IN(485,565,308,307,399,402,556,494,131,17,525,182,335,559,553,552,560,333,414,405,406,228,229,511,116,412,39,397,475,351,349,42,348,391,534,500,11,362,6,288,421,390,370,195);
- -- 删除存储过程
- -- drop procedure if exists begin_sync_org;
- -- drop procedure if exists sync_org;
|