zoukankan      html  css  js  c++  java
  • mysql5.7 虚拟列存储过程

    BEGIN
    DECLARE Done INT DEFAULT 0;
    DECLARE v_tablename VARCHAR(50);
    
    DECLARE rs CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'wd' AND TABLE_NAME REGEXP '^(page_).*_.*';
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done=1;
    OPEN rs;
    FETCH NEXT FROM rs INTO v_tablename;
    REPEAT
    IF NOT Done THEN
    
    set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " ADD `creatTime`  varchar (30) GENERATED ALWAYS  AS (replace(json_extract(info,  '$.creatTime' ),'"','' ) ) VIRTUAL");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " ADD `lastScanTime`  varchar (30) GENERATED ALWAYS  AS (replace(json_extract(info,  '$.lastScanTime' ),'"','') ) VIRTUAL");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " ADD `statusCode`     INT(6)  GENERATED ALWAYS  AS (json_extract(info,  '$.statusCode' ))   VIRTUAL");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " ADD `pageType`     varchar(15)  GENERATED ALWAYS  AS (replace(json_extract(info,  '$.pageType' ),'"','') )   VIRTUAL");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("CREATE INDEX idx_createTime  ON " , v_tablename , "(creatTime)");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("CREATE INDEX idx_lastScanTime ON ",v_tablename,"(lastScanTime)");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("CREATE INDEX idx_statusCode  ON " , v_tablename , "(statusCode)");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    
    set @sqllang = CONCAT("CREATE INDEX idx_pageType  ON " , v_tablename , "(pageType)");
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
     
    END IF;
    FETCH NEXT FROM rs INTO v_tablename;
    
    UNTIL Done END REPEAT;
    CLOSE rs;
    end
    CREATE DEFINER = 'root'@'%'
    PROCEDURE wd.dropColumnAndIndex()
    BEGIN
    DECLARE Done INT DEFAULT 0;
    DECLARE v_tablename VARCHAR(50);
    DECLARE v_row int DEFAULT 0; 
    
    DECLARE rs CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'wd' AND TABLE_NAME REGEXP '^(page_).*_.*';
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done=1;
    OPEN rs;
    FETCH NEXT FROM rs INTO v_tablename;
    REPEAT
    IF NOT Done THEN
    
    set @sqllang = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.statistics  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND INDEX_NAME= 'idx_createTime'");  
    PREPARE stmt FROM @sqllang;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " DROP INDEX `idx_createTime` ");
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
    
    set @sqlexist = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.statistics  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND INDEX_NAME= 'idx_lastScanTime'");  
    PREPARE stmt FROM @sqlexist;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " DROP INDEX `idx_lastScanTime` ");
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
    
    set @sqlexist = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.statistics  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND INDEX_NAME= 'idx_statusCode'");  
    PREPARE stmt FROM @sqlexist;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " DROP INDEX `idx_statusCode` "); 
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
    
    set @sqlexist = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.Columns  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND column_name= 'creatTime'");  
    PREPARE stmt FROM @sqlexist;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " drop column creatTime ");
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
    
    set @sqlexist = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.Columns  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND column_name= 'lastScanTime'");  
    PREPARE stmt FROM @sqlexist;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " DROP COLUMN  lastScanTime ");
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
    
    set @sqlexist = CONCAT("SELECT count(1) into @row FROM INFORMATION_SCHEMA.Columns  WHERE table_schema= 'wd' AND table_name= '" ,v_tablename,"' AND column_name= 'statusCode'");  
    PREPARE stmt FROM @sqlexist;  
    EXECUTE stmt;
    if (@row > 0) then
        set @sqllang = CONCAT("ALTER TABLE " , v_tablename ,  " DROP COLUMN statusCode ");
        PREPARE stmt FROM @sqllang;  
        EXECUTE stmt;
    end if;
     
    END IF;
    FETCH NEXT FROM rs INTO v_tablename;
    
    UNTIL Done END REPEAT;
    CLOSE rs;
    end
  • 相关阅读:
    如何在perl中一次执行多条shell命令
    Spring.netAOP 搭建网站通知服务(1)
    使用Spring.net AOP 实现积分服务
    我得第一帖@2011
    ADO.NET+存储过程+事务的一个奇怪问题,求探讨
    (一)项目说明及程序框架说明——.NET开发完整案例(企业邮箱系统)
    2.系统设置网站配置网站信息配置
    .NET企业邮箱系统项目说明及文章索引——20118.13更新
    .Net内容管理系统开发实例项目说明及文章索引——2011825更新
    后端小记录
  • 原文地址:https://www.cnblogs.com/leonkobe/p/7286171.html
Copyright © 2011-2022 走看看