存储过程批量删除多个子表中的重复数据

2023-11-29 19:08

本文主要是介绍存储过程批量删除多个子表中的重复数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

``````
use database;
set names utf8;-- 删除客户表子表中的重复数据
DELIMITER //
drop procedure if exists delFieldPro// 
CREATE PROCEDURE delFieldPro()  
BEGIN  DECLARE tableName varchar(100) default '';   DECLARE done INT DEFAULT 0;  DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='uc_ipcc_cloud_ai' and table_name like  'tb_customer_%' and table_name not like  'tb_customer_contact%' ;  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  OPEN taskCursor;  REPEAT  FETCH taskCursor INTO tableName;  IF not done THEN  set @sql0=concat('delete from tb_tmp;') ;PREPARE stmt0 from @sql0; execute stmt0;set @sql=concat('insert into tb_tmp select t.id from ( select max(id) id,customer_nbr,count(1) cu from ',tableName,'  group by customer_nbr having cu>1) t') ;PREPARE stmt from @sql; execute stmt;set @sql1=concat('delete from ',tableName,'  where id in (select id from tb_tmp)');PREPARE stmt1 from @sql1; execute stmt1;END IF;  UNTIL done END REPEAT;  CLOSE taskCursor;
END 
//
DELIMITER ;
CALL delFieldPro();-- 给客户子表添加唯一索引
DELIMITER //
drop procedure if exists addUniqueIndexPro// 
CREATE PROCEDURE addUniqueIndexPro()  
BEGIN  DECLARE tableName varchar(100) default '';   DECLARE done INT DEFAULT 0;  DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='database' and table_name like  'tb_customer_%' and table_name not like  'tb_ucpaas_customer_contact%' ;  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  OPEN taskCursor;  REPEAT  FETCH taskCursor INTO tableName;  IF not done THEN  set @sql0=concat('ALTER TABLE  ',tableName,' ADD unique(customer_nbr);') ;PREPARE stmt0 from @sql0; execute stmt0;      END IF;  UNTIL done END REPEAT;  CLOSE taskCursor;
END 
//
DELIMITER ;
CALL addUniqueIndexPro();-- 找出客户表中含有重复号码的表
DELIMITER //
drop procedure if exists proc_find_repeatNum;
create procedure proc_find_repeatNum (out sum varchar(1000))
BEGINDECLARE tableName varchar(100) default '';   DECLARE done INT DEFAULT 0; DECLARE _num int DEFAULT 0;DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='database' and table_name like  'tb_customer_%' and table_name not like  'tb_ucpaas_customer_contact%' ;  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  OPEN taskCursor;  REPEAT  FETCH taskCursor INTO tableName;  IF not done THEN  set @x ='';set @sql0=concat('select count(1) into @tnum from  (select customer_nbr,count(1) cu from ',tableName,' group by customer_nbr having cu>1) t;') ;PREPARE stmt0 from @sql0; execute stmt0;  deallocate prepare stmt0;SET _num = @tnum;if(_num > 0)then  SET sum= concat(tableName,',');END IF;END IF;  UNTIL done END REPEAT;  CLOSE taskCursor;
end
//
DELIMITER ;
call proc_find_repeatNum(@sum);
-- 查询结果集
select @sum;---删除某张表的重复数据
select count(1) from  (select customer_nbr,count(1) cu from tb_customer_ddd92456d085c9dd4976b59c90e48de6 group by customer_nbr having cu>1) t;
delete from tb_tmp;
insert into tb_tmp select t.id from ( select max(id) id,customer_nbr,count(1) cu from tb_customer_ddd92456d085c9dd4976b59c90e48de6  group by customer_nbr having cu>1) t ;
delete from tb_customer_ddd92456d085c9dd4976b59c90e48de6 where id in (select id from tb_tmp);``````

-- 删除多余索引
DELIMITER //
drop procedure if exists delUniqueIndexPro// 
CREATE PROCEDURE delUniqueIndexPro()  
BEGIN  DECLARE tableName varchar(100) default '';   DECLARE done INT DEFAULT 0;  DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='uc_ipcc_cloud_ai' and table_name like  'tb_ucpaas_customer_%' and table_name not like  'tb_ucpaas_customer_contact%' ;  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  OPEN taskCursor;  REPEAT  FETCH taskCursor INTO tableName;  IF not done THEN  IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='uc_ipcc_cloud_ai' AND table_name = tableName AND index_name = 'customer_nbr_2') THENset @sql0=concat('ALTER TABLE  ',tableName,' drop index customer_nbr_2 ;') ;PREPARE stmt0 from @sql0; execute stmt0; END IF;IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='uc_ipcc_cloud_ai' AND table_name = tableName AND index_name = 'customer_nbr_3') THENset @sql0=concat('ALTER TABLE  ',tableName,' drop index customer_nbr_3;') ;PREPARE stmt0 from @sql0; execute stmt0; END IF;IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='uc_ipcc_cloud_ai' AND table_name = tableName AND index_name = 'customer_nbr_4') THENset @sql0=concat('ALTER TABLE  ',tableName,' drop index customer_nbr_4;') ;PREPARE stmt0 from @sql0; execute stmt0; END IF;IF EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='uc_ipcc_cloud_ai' AND table_name = tableName AND index_name = 'idx_customer_nbr') THENset @sql0=concat('ALTER TABLE  ',tableName,' drop index idx_customer_nbr;') ;PREPARE stmt0 from @sql0; execute stmt0; END IF;  END IF;UNTIL done END REPEAT;  CLOSE taskCursor;
END 
//
DELIMITER ;
CALL delUniqueIndexPro();

这篇关于存储过程批量删除多个子表中的重复数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/433964

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

将Mybatis升级为Mybatis-Plus的详细过程

《将Mybatis升级为Mybatis-Plus的详细过程》本文详细介绍了在若依管理系统(v3.8.8)中将MyBatis升级为MyBatis-Plus的过程,旨在提升开发效率,通过本文,开发者可实现... 目录说明流程增加依赖修改配置文件注释掉MyBATisConfig里面的Bean代码生成使用IDEA生

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分