• mysql存储过程分库分表



    -- 存储过程创建库  分为两条语句删除和创建
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS createDBN $$
    CREATE PROCEDURE createDBN(IN n INT,IN dbName VARCHAR(100),IN dropSql VARCHAR(255),IN createSql VARCHAR(255))
    BEGIN
      DECLARE i INT DEFAULT 0;
      WHILE i<n DO
        SET @tempName = CONCAT(dbName,CONVERT(i,CHAR(10)));
        SET @dropTempSql = REPLACE(dropSql,'${tbTempName}',@tempName);
        SET @dropTempSql2 = @dropTempSql;
        PREPARE dropSTMT FROM @dropTempSql2;
        EXECUTE dropSTMT;
        DEALLOCATE PREPARE dropSTMT;
     
        SET @createTempSql = REPLACE(createSql,'${tbTempName}',@tempName);
        SET @createTempSql2 = @createTempSql;
        PREPARE createSTMT FROM @createTempSql2;
        EXECUTE createSTMT;
        DEALLOCATE PREPARE createSTMT; 
     
        SET i=i+1;
      END WHILE;
    END $$
     
    SET @dropSql = "DROP DATABASE IF EXISTS ${tbTempName};";
    SET @creSql = "CREATE database ${tbTempName} character set=utf8;";
    CALL createDBN(5,'tl',@dropSql,@creSql);
     
     
    -- 存储过程  批量删除数据库
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS delDBN $$
    CREATE PROCEDURE delDBN(IN dbName VARCHAR(50),IN startValue INT,IN endValue INT)
    BEGIN
      DECLARE i INT DEFAULT 0;
      DECLARE tempName VARCHAR(50);
      SET i = startValue;
      WHILE i<endValue DO
        SET tempName=CONCAT(dbName,CONVERT(i,CHAR(10)));
        SET @exeSql = CONCAT('drop database if exists ',tempName);
        PREPARE exeSTMT FROM @exeSql;
        EXECUTE exeSTMT;
        DEALLOCATE PREPARE exeSTMT;
      SET i=i+1;
      END WHILE;
    END $$
     
    CALL delDBN('tl',0,5);
     
     
    -- 存储过程  批量创建数据库
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS createDBN2 $$
    CREATE PROCEDURE createDBN2(IN n INT,IN dbName VARCHAR(100))
    BEGIN
       DECLARE i INT DEFAULT 0;
       DECLARE tempName VARCHAR(50);
       WHILE i<n DO
          SET tempName = CONCAT(dbName,CAST(i AS CHAR(10)));
          SET @exeDropSql = CONCAT('drop database if exists ',tempName);
          SET @exeCreSql = CONCAT('create database ',tempName,' character set=utf8');
     
          -- 删除
          PREPARE exeDropSTMT FROM @exeDropSql;
          EXECUTE exeDropSTMT;
          DEALLOCATE PREPARE exeDropSTMT;
     
          -- 创建
          PREPARE exeCreSTMT FROM @exeCreSql;
          EXECUTE exeCreSTMT;
          DEALLOCATE PREPARE exeCreSTMT;
     
       SET i=i+1;
       END WHILE;
    END $$
     
    CALL createDBN2(5,'tl');
     
     
    -- 存储过程  批量创建表
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS createTabN $$
    CREATE PROCEDURE createTabN(IN dbN INT,IN tabN INT,IN dbName VARCHAR(50),IN tabName VARCHAR(50),IN creTabSql VARCHAR(1000))
    BEGIN
      DECLARE i INT DEFAULT 0;
      DECLARE j INT DEFAULT 0;
      DECLARE tempName VARCHAR(100);
      WHILE i<dbN DO
         WHILE j<tabN DO
            SET tempName = CONCAT(dbName,CONVERT(i,CHAR(10)),'.',tabName,CAST(j AS CHAR(10)));
            SET @dropTabSql = CONCAT('drop table if exists ',tempName);
            SET @exeSql = REPLACE(creTabSql,'${fullTempName}',tempName);
            -- 删除表      
            PREPARE dropSTMT FROM @dropTabSql;
            EXECUTE dropSTMT;
            DEALLOCATE PREPARE dropSTMT;
     
            -- 创建表
            PREPARE exeSTMT FROM @exeSql;
            EXECUTE exeSTMT;
            DEALLOCATE PREPARE exeSTMT;
     
            SET j=j+1;
         END WHILE;
         SET i=i+1;
         SET j=0;
      END WHILE;
     
    END $$
     
    SET @tsql="
       create table ${fullTempName}(
          id int not null primary key auto_increment,
          lname varchar(10) 
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
    ";
     
    CALL createTabN(5,10,'tl','ll',@tsql);
     
     
    -- 存储过程  批量删除表
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS delTabN $$
    CREATE PROCEDURE delTabN(IN dbN INT,IN tabN INT,IN dbName VARCHAR(50),IN tabName VARCHAR(50))
    BEGIN
      DECLARE i INT DEFAULT 0;
      DECLARE j INT DEFAULT 0;
      WHILE i<dbN DO
         WHILE j<tabN DO
           SET @tempName=CONCAT(dbName,CONVERT(i,CHAR(10)),'.',tabName,CAST(j AS CHAR(10)));
           SET @dropSql=CONCAT('drop table if exists ',@tempName);
     
           PREPARE dropSTMT FROM @dropSql;
           EXECUTE dropSTMT;
           DEALLOCATE PREPARE dropSTMT;     
     
           SET j=j+1;
         END WHILE;
         SET j=0;
         SET i=i+1;
      END WHILE;
    END $$
     
    CALL delTabN(5,10,'tl','ll');
     
     
     
    -- 存储过程  批量创建库 创建表
    DELIMITER $$
    USE myplan $$
    DROP PROCEDURE IF EXISTS creDBAndTabN $$
    CREATE PROCEDURE creDBAndTabN(IN dbN INT,IN tabN INT,IN dbName VARCHAR(50),IN tabName VARCHAR(50),IN tabSql VARCHAR(500))
    BEGIN
       DECLARE i INT DEFAULT 0;
       DECLARE j INT DEFAULT 0;
       WHILE i<dbN DO
          SET @tempDBName = CONCAT(dbName,CAST(i AS CHAR(10)));
          SET @dropDBSql = CONCAT('drop database if exists ',@tempDBName);
          SET @creDBSql = CONCAT('create database ',@tempDBName,' character set=utf8');
     
          -- 删除库
          PREPARE dropDBSTMT FROM @dropDBSql;
          EXECUTE dropDBSTMT;
          DEALLOCATE PREPARE dropDBSTMT;
     
          -- 创建库
          PREPARE creDBSTMT FROM @creDBSql;
          EXECUTE creDBSTMT;
          DEALLOCATE PREPARE creDBSTMT;
     
          -- 创建表
          WHILE j<tabN DO
            SET @tempTabName = CONCAT(dbName,CAST(i AS CHAR(10)),'.',tabName,CAST(j AS CHAR(10)));
            SET @dropTabSql = CONCAT('drop table if exists ',@tempTabName);
            SET @creTabSql = REPLACE(tabSql,'${fullTabName}',@tempTabName);
     
            -- 删除表
            PREPARE dropTabSTMT FROM @dropTabSql;
            EXECUTE dropTabSTMT;
            DEALLOCATE PREPARE dropTabSTMT;
     
            -- 创建表
            PREPARE creTabSTMT FROM @creTabSql;
            EXECUTE creTabSTMT;
            DEALLOCATE PREPARE creTabSTMT;     
     
            SET j=j+1;
          END WHILE;
     
          SET i=i+1;
          SET j=0;
       END WHILE;
    END $$
     
    SET @tsql="
       create table ${fullTabName}(
          id int not null primary key auto_increment,
          lname varchar(10) 
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
    ";
     
    CALL creDBAndTabN(5,10,'tl','ll',@tsql);
     
     

  • 相关阅读:
    微软BI 之SSAS 系列
    微软BI 之SSIS 系列
    微软BI 之SSAS 系列
    微软BI 之SSAS 系列
    微软BI 之SSIS 系列
    微软BI 之SSRS 系列
    微软BI 之SSAS 系列
    微软BI 之SSAS 系列
    微软BI 之SSAS 系列
    微软BI 之SSRS 系列
  • 原文地址:https://www.cnblogs.com/tlll/p/3450533.html
Copyright © 2020-2023  润新知