zoukankan      html  css  js  c++  java
  • 84、给mysql数据库中的所有表批量添加字段

    DROP PROCEDURE IF EXISTS addColumn;
    DELIMITER $$
     
     CREATE PROCEDURE addColumn()
    BEGIN
      -- 定义表名变量
      DECLARE s_tablename VARCHAR(100);
     
     /*显示表的数据库中的所有表
     SELECT table_name FROM information_schema.tables WHERE table_schema='databasename' Order by table_name ;
     */
     
    #显示所有
     DECLARE cur_table_structure CURSOR
     FOR 
     SELECT table_name 
     FROM INFORMATION_SCHEMA.TABLES 
     -- test = 数据库名称
     WHERE table_schema = 'you db' AND table_name NOT IN (
     SELECT t.table_name  FROM (
       SELECT table_name,column_name FROM information_schema.columns 
       WHERE table_name IN ( 
        SELECT table_name 
        FROM INFORMATION_SCHEMA.TABLES 
        WHERE table_schema = 'ag_nacos')
       ) t WHERE t.column_name='object_name' 
     );
     
     DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s_tablename = NULL;
     
     OPEN cur_table_structure;
     
     FETCH cur_table_structure INTO s_tablename;
     
     WHILE ( s_tablename IS NOT NULL) DO
      SET @MyQuery=CONCAT("alter table `",s_tablename,"` add COLUMN `age` INT COMMENT '年龄'");
      PREPARE msql FROM @MyQuery;
      
      EXECUTE msql ;#USING @c; 
       
      FETCH cur_table_structure INTO s_tablename;
      END WHILE;
     CLOSE cur_table_structure;
     
     
    END;
     $$
     
     #执行存储过程
     CALL addColumn();
    

      

  • 相关阅读:
    正则表达式 \n和\r
    【转】单循环赛赛程安排算法研究
    Iterator效率
    Map获取键值
    PL/SQL语法详解(pdf)
    Iterator模式
    测试js函数的静态页面
    【转】java的一些基本概念
    Oracle 11g用户解锁
    oracle官方文档
  • 原文地址:https://www.cnblogs.com/gfbzs/p/14741531.html
Copyright © 2011-2022 走看看