美文网首页
mysql给所有表加字段--存储过程

mysql给所有表加字段--存储过程

作者: 少年眼蓝不及海 | 来源:发表于2022-10-20 10:28 被阅读0次
CREATE DEFINER=`root`@`%` PROCEDURE `useCursor`()
BEGIN
    DECLARE oneAddr varchar(100) default '';
    DECLARE allAddr varchar(100) default '';
    DECLARE done INT DEFAULT 0;
    DECLARE curl CURSOR FOR select table_name from information_schema.tables where table_schema='test';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    OPEN curl;
    REPEAT
      FETCH curl INTO oneAddr;
      IF not done THEN
               if (select count(*) from information_schema.columns where table_name = oneAddr and column_name = 'form_key') = 0 THEN
                     set @sql=concat('alter table ',oneAddr,' add (form_key VARCHAR(120) COMMENT "表单键值")');
                     PREPARE stmt from @sql;
                     execute stmt;
                 END IF;
                 if (select count(*) from information_schema.columns where table_name = oneAddr and column_name = 'form_table_code') = 0 THEN
                     set @sqlA=concat('alter table ',oneAddr,' add (form_table_code VARCHAR(120) COMMENT "表单配置表名表code")');
                     PREPARE stmtA from @sqlA;
                     execute stmtA;
                 END IF;
                
      END IF;
    UNTIL done END REPEAT;
    select allAddr;
    CLOSE curl;
  END

mysql insert+selecte 主键用uuid()

INSERT INTO xxx(id,name) 
SELECT md5(UUID()) id, name from xxx

相关文章

网友评论

      本文标题:mysql给所有表加字段--存储过程

      本文链接:https://www.haomeiwen.com/subject/eaiqirtx.html