wakasann
1/11/2019 - 4:33 AM

mysql use sql batch add column or delete column(deleted_at,created_at,updated_at)

drop procedure if exists delete_timestamp;  
CREATE PROCEDURE delete_timestamp()  
  BEGIN  
    DECLARE tableName varchar(30) default '';  
    DECLARE allAddr varchar(40) default '';
    DECLARE tableSchemaName varchar(30) default 'test';
    DECLARE done INT DEFAULT 0;  
    DECLARE curl CURSOR FOR select table_name from information_schema.tables where table_schema=tableSchemaName;  
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  
    OPEN curl;  
    REPEAT  
      FETCH curl INTO tableName;  
      IF not done THEN
         IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'deleted_at') THEN
           set @sql=concat('alter table ',tableName,' DROP  `deleted_at`');
           PREPARE stmt from @sql;
           execute stmt;
         END IF;
         
         IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'created_at') THEN
           set @sql2=concat('alter table ',tableName,' DROP  `created_at`');
           PREPARE stmt2 from @sql2; 
           execute stmt2;
         END IF;
				 
				 IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'updated_at') THEN
           set @sql3=concat('alter table ',tableName,' DROP  `updated_at`');
           PREPARE stmt3 from @sql3; 
           execute stmt3;
         END IF;
         
      END IF;  
    UNTIL done END REPEAT;  
    select allAddr;
    CLOSE curl;     
END;  

call delete_timestamp(); 
drop procedure if exists add_all_timestamp;  
CREATE PROCEDURE add_all_timestamp()  
  BEGIN  
    DECLARE tableName varchar(30) default '';  
    DECLARE allAddr varchar(40) default '';
    DECLARE tableSchemaName varchar(30) default 'test';
    DECLARE done INT DEFAULT 0;  
    DECLARE curl CURSOR FOR select table_name from information_schema.tables where table_schema=tableSchemaName;  
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  
    OPEN curl;  
    REPEAT  
      FETCH curl INTO tableName;  
      IF not done THEN
         IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'deleted_at') THEN
           set @sql=concat('alter table ',tableName,' ADD  `deleted_at` TIMESTAMP NULL');
           PREPARE stmt from @sql;
           execute stmt;
         END IF;
         
         IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'created_at') THEN
           set @sql2=concat('alter table ',tableName,' ADD  `created_at` TIMESTAMP NULL');
           PREPARE stmt2 from @sql2; 
           execute stmt2;
         END IF;
				 
				 IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'updated_at') THEN
           set @sql3=concat('alter table ',tableName,' ADD  `updated_at` TIMESTAMP NULL');
           PREPARE stmt3 from @sql3; 
           execute stmt3;
         END IF;
         
      END IF;  
    UNTIL done END REPEAT;  
    select allAddr;
    CLOSE curl;     
END;  

call add_all_timestamp();