指定值在数据库中所有表所有字段值的替换(存储过程):指定值在数据库中所有表所有字段值的替换(存储过程): 1.写一个存储过程,查出指定数据库中所有的表名: CREATE DEFINER=`root`@`localhost` PROCEDURE `init_replace`(in orig_str varchar(255),in new_str varchar(255),in db_name varchar(100))BEGIN #Routine body goes here...DECLARE
1.写一个存储过程,查出指定数据库中所有的表名:
CREATE DEFINER=`root`@`localhost` PROCEDURE `init_replace`(in orig_str varchar(255),in new_str varchar(255),in db_name varchar(100))
BEGIN
#Routine body goes here...
DECLARE t_name VARCHAR(100);
DECLARE done int default 0;
DECLARE cur CURSOR FOR SELECT DISTINCT table_name as name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=db_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
FETCH cur INTO t_name;
WHILE (done<>1) do
call do_replace(orig_str,new_str,db_name,t_name);
FETCH cur INTO t_name;
END WHILE;
END
2.然后1中调用2中的方法,再查询表中所有的字段,并且指定更新值:
CREATE DEFINER=`root`@`localhost` PROCEDURE `do_replace`(in orig_str varchar(100),in new_str varchar(100),in db_name varchar(100),in t_name varchar(100))
BEGIN
#Routine body goes here...
DECLARE cul_name VARCHAR(50);
DECLARE done int default 0;
DECLARE cur CURSOR FOR SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA=db_name and TABLE_NAME=t_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
FETCH cur INTO cul_name;
WHILE (done<>1) do
set @update_sql=CONCAT("Update `",t_name,"` SET `",cul_name,"` =REPLACE(`",cul_name,"`,'",orig_str,"','",new_str,"');");
prepare stmt from @update_sql;
execute stmt;
FETCH cur INTO cul_name;
END WHILE;
CLOSE cur;
END
3.调用1的方法:启动存储过程:前边是要替换的只,后边是替换后的指
call init_replace('http://www.baidu.com','https://www.baidu.com','给数据库名称');
沃梦达教程
本文标题为:mysql数据库中替换所有表中的所有字段值(存储过程)
猜你喜欢
- Oracle 删除大量表记录操作分析总结 2023-07-23
- redis清除数据 2023-09-13
- 在阿里云CentOS 6.8上安装Redis 2023-09-12
- 基于Python制作一个简单的文章搜索工具 2023-07-28
- 搭建单机Redis缓存服务的实现 2023-07-13
- Mongodb启动报错完美解决方案:about to fork child process,waiting until server is ready for connections. 2023-07-16
- SQLSERVER调用C#的代码实现 2023-07-29
- Numpy中如何创建矩阵并等间隔抽取数据 2023-07-28
- MySQL8.0.28安装教程详细图解(windows 64位) 2023-07-26
- SQL Server 2022 AlwaysOn新特性之包含可用性组详解 2023-07-29