mysql数据库,如何实现全表、全字段替换呢?
答案:4 悬赏:50 手机版
解决时间 2021-01-29 22:17
- 提问者网友:遁入空寂
- 2021-01-29 12:38
请6rl老师回答,感谢!!
最佳答案
- 五星知识达人网友:洒脱疯子
- 2021-01-29 13:58
--test库是目标库(做替换前一定要做好备份)
--test2是图片名字数据集表所在的库
use test2
-- 建立测试表和数据
create table picName (oldname varchar(200),newname varchar(200));
insert into picName values ('图片.JPG','tupian.JPG'),('博客.JPG','boke.JPG'),('网站.JPG','wangzhan.JPG');
commit;
-- 建立存储过程
drop procedure if exists dynamic_cursor;
CREATE PROCEDURE dynamic_cursor (IN db_name varchar(100))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE v_updateSql varchar(2000);
DECLARE times INT DEFAULT 0;
--
--
declare _err int default 0;
DECLARE cur CURSOR for( SELECT updateSql from picName_view);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION set _err=1;
DROp VIEW IF EXISTS picName_view;
SET @sqlstr = "CREATE VIEW picName_view as ";
-- SET @sqlstr = "";
SET @sqlstr =CONCAt(@sqlstr ,"select concat('update ",db_name,".`',a.table_name,'` set ',a.column_name,'=replace(',a.column_name,',''',b.oldname,''',''',b.newname,''')') updateSql");
SET @sqlstr =CONCAt(@sqlstr ," from information_schema.`COLUMNS` a ,picName b ");
SET @sqlstr =CONCAt(@sqlstr ," where a.TABLE_SCHEMA='",db_name,"' and ");
SET @sqlstr =CONCAt(@sqlstr ," (a.COLUMN_type like '%char%' or a.COLUMN_type like '%text%' or a.COLUMN_type like '%blob%')");
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OPEN cur;
f_loop:LOOP
FETCH cur INTO v_updateSql;
IF done THEN
LEAVE f_loop;
END IF;
set @updateSql=v_updateSql;
PREPARE stmt2 FROM @updateSql;
EXECUTE stmt2;
set times=times+1;
-- SELECT v_updateSql;
END LOOP f_loop;
CLOSE cur;
COMMIT;
select times;
END;
-- 测试
call dynamic_cursor('test');
--test2是图片名字数据集表所在的库
use test2
-- 建立测试表和数据
create table picName (oldname varchar(200),newname varchar(200));
insert into picName values ('图片.JPG','tupian.JPG'),('博客.JPG','boke.JPG'),('网站.JPG','wangzhan.JPG');
commit;
-- 建立存储过程
drop procedure if exists dynamic_cursor;
CREATE PROCEDURE dynamic_cursor (IN db_name varchar(100))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE v_updateSql varchar(2000);
DECLARE times INT DEFAULT 0;
--
--
declare _err int default 0;
DECLARE cur CURSOR for( SELECT updateSql from picName_view);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION set _err=1;
DROp VIEW IF EXISTS picName_view;
SET @sqlstr = "CREATE VIEW picName_view as ";
-- SET @sqlstr = "";
SET @sqlstr =CONCAt(@sqlstr ,"select concat('update ",db_name,".`',a.table_name,'` set ',a.column_name,'=replace(',a.column_name,',''',b.oldname,''',''',b.newname,''')') updateSql");
SET @sqlstr =CONCAt(@sqlstr ," from information_schema.`COLUMNS` a ,picName b ");
SET @sqlstr =CONCAt(@sqlstr ," where a.TABLE_SCHEMA='",db_name,"' and ");
SET @sqlstr =CONCAt(@sqlstr ," (a.COLUMN_type like '%char%' or a.COLUMN_type like '%text%' or a.COLUMN_type like '%blob%')");
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
OPEN cur;
f_loop:LOOP
FETCH cur INTO v_updateSql;
IF done THEN
LEAVE f_loop;
END IF;
set @updateSql=v_updateSql;
PREPARE stmt2 FROM @updateSql;
EXECUTE stmt2;
set times=times+1;
-- SELECT v_updateSql;
END LOOP f_loop;
CLOSE cur;
COMMIT;
select times;
END;
-- 测试
call dynamic_cursor('test');
全部回答
- 1楼网友:鸽屿
- 2021-01-29 16:36
--test库是目标库(做替换前一定要做好备份)
--test2是图片名字数据集表所在的库
use test2
-- 建立测试表和数据
create table picName (oldname varchar(200),newname varchar(200));
insert into picName values ('图片.JPG','tupian.JPG'),('博客.JPG','boke.JPG'),('网站.JPG','wangzhan.JPG');
commit;
-- 建立存储过程
drop procedure if exists dynamic_cursor;
CREATE PROCEDURE dynamic_cursor (IN db_name varchar(100))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE v_updateSql varchar(2000);
DECLARE times INT DEFAULT 0;
- 2楼网友:長槍戰八方
- 2021-01-29 16:21
根据列名找表?
use information_schema;
select * from columns where column_name='字段名' ;
- 3楼网友:低音帝王
- 2021-01-29 15:18
insert 表A select * from 表B
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯