MySQL使用存储过程批量更新数据库所有表某个字段值
时间:2021-07-01 10:21:17
帮助过:12人阅读
DROP PROCEDURE IF EXISTS updateColumn;
CREATE PROCEDURE updateColumn()
BEGIN
DECLARE flag INT DEFAULT 0;
DECLARE tname VARCHAR(50);
DECLARE result CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.Columns WHERE TABLE_SCHEMA = ‘sens_blog‘ AND COLUMN_NAME = ‘del_flag‘;
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET flag = 1;
OPEN result;
WHILE flag <> 1 DO
FETCH result INTO tname;
SET @execSql = CONCAT(‘ALTER TABLE ‘, tname, ‘ ALTER COLUMN del_flag SET DEFAULT 0‘);
PREPARE stmt FROM @execSql;
EXECUTE stmt;
END WHILE;
END;
CALL updateColumn();
如果你想做其他的操作,只需要修改22行,改成你的SQL语句就行,当然数据库名和字段名也要改。
MySQL使用存储过程批量更新数据库所有表某个字段值
标签:into sel png concat where width prepare 循环条件 cursor