• 存储过程—增减字段


    情景:由于公司业务需要,需要对大批量表进行字段修改,这些表的表名类似:cms_new_0 cms_new_1 cms_new_2...

    新建存储过程,批量删除字段 city_id

    BEGIN  
        DECLARE tableName varchar(100) default '';   
        DECLARE done INT DEFAULT 0;  
        DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='zhaoshayou_fabuz' and table_name like  'cms_new_%';  
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  
        OPEN taskCursor;  
        REPEAT  
          FETCH taskCursor INTO tableName;  
          IF not done THEN  
    			IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='zhaoshayou_fabuz' and TABLE_NAME = tableName AND COLUMN_NAME = 'city_id' )THEN
    		 	set @sql2=concat('ALTER TABLE  ',tableName,' drop city_id');
             PREPARE stmt from @sql2;
             execute stmt;		 
     			 END IF;
          END IF;  
        UNTIL done END REPEAT;  
        CLOSE taskCursor;
    END
    

    新建存储过程,批量添加字段 province_id

    BEGIN  
        DECLARE tableName varchar(100) default '';   
        DECLARE done INT DEFAULT 0;  
        DECLARE taskCursor CURSOR FOR select table_name from information_schema.tables where table_schema='zhaoshayou_fabuz' and table_name like  'cms_new_%';  
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  
        OPEN taskCursor;  
        REPEAT  
          FETCH taskCursor INTO tableName;  
          IF not done THEN  
    			IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='zhaoshayou_fabuz' and TABLE_NAME = tableName AND COLUMN_NAME = 'province_id' )THEN
    		 	set @sql2=concat('ALTER TABLE  ',tableName,' ADD province_id int(11) DEFAULT 0');
             PREPARE stmt from @sql2;
             execute stmt;		 
     			 END IF;
          END IF;  
        UNTIL done END REPEAT;  
        CLOSE taskCursor;
    END
  • 相关阅读:
    python线程同步
    闭包、装饰器
    python中的return返回值
    常用内建函数
    python迭代器与生成器
    python基础之数据类型
    VS生成解决方案时报错: Your project does not reference ".NETFramework,Version=v4.5"
    Unity 低版本打开高版本项目
    关闭应用程序的所有子窗体
    按钮接收 Enter 键
  • 原文地址:https://www.cnblogs.com/wangyuyanhello/p/14200704.html
Copyright © 2020-2023  润新知