DROP PROCEDURE IF EXISTS updateColumn;
CREATE PROCEDURE updateColumn(in orig_str varchar(100),in new_str varchar(100),in db_name varchar(100),in cul_name varchar(100))
BEGIN
-- 定义循环条件
DECLARE flag INT DEFAULT 0;
DECLARE tname VARCHAR(50);
-- 查询指定数据库中含有指定列的表
DECLARE result CURSOR FOR SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA = db_name AND COLUMN_NAME = cul_name AND TABLE_NAME not in ('vw_credit_account', 'vw_ele_agency');
-- 退出循环
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET flag = 1;
-- 打开游标
OPEN result;
WHILE flag <> 1 DO
-- 游标指向下一个位置,可以有多个数据,比如FETCH result INTO tname,ttype,...;
FETCH result INTO tname;
-- 拼接字符串表名sql,根据需要使用CONCAT函数连接
-- 批量设置所有表的为del_flag字段0
SET @execSql = CONCAT('UPDATE ', tname, ' SET mof_div_code = 350200000, fic_year = 2022');
-- set @execSql=CONCAT('UPDATE ',tname,' SET ',cul_name,'=replace(',cul_name,',''',orig_str,''',''',new_str,''');');
-- 批量设置所有表的为del_flag字段默认值为0
-- SET @execSql = CONCAT('ALTER TABLE ', tname, ' ALTER COLUMN del_flag SET DEFAULT 0');
PREPARE stmt FROM @execSql;
EXECUTE stmt;
END WHILE;
END;
CALL updateColumn('220221222223','12345','internal_control','mof_div_code');
参考:
MySQL存储过程批量更新数据库所有表某个字段值_千枝万叶的博客-CSDN博客_mysql批量更新某个字段的值
|