1. 程式人生 > 其它 >91、mysql批量刪除表

91、mysql批量刪除表

## 儲存過程實現
drop PROCEDURE if EXISTS rush;
create PROCEDURE rush()
BEGIN 

## 建立臨時表,插入快照資料
drop table if exists drop_tb;
create TEMPORARY table drop_tb(
rowNum int not null,
table_name VARCHAR(50) not null
);
insert into drop_tb       
      select @r := @r + 1 as rowNum,
             table_name
      from information_schema.TABLES as a,(select @r := 0 )as t
      where table_schema = (select DATABASE())
			
      and table_name like 'c%' ##這個地方填寫以什麼開頭的資料
      order by a.table_name ;

## 儲存過程實現
drop PROCEDURE if EXISTS rush;
create PROCEDURE rush()
BEGIN 

## 建立臨時表,插入快照資料
drop table if exists drop_tb;
create TEMPORARY table drop_tb(
rowNum int not null,
table_name VARCHAR(50) not null
);
insert into drop_tb       
      select @r := @r + 1 as rowNum,
             table_name
      from information_schema.TABLES as a,(select @r := 0 )as t
      where table_schema = (select DATABASE())
			
      and table_name like 'c%' ##這個地方填寫以什麼開頭的資料
      order by a.table_name ;

## 變數設定
set @index = 0;
set @count = (select count(0) from drop_tb) ;

## 遍歷刪除字首為 aopi_copy 的表
WHILE @index <  @count DO
        
    set @index = @index + 1 ;
    set @tb_name = (
        select table_name from drop_tb as ibn
        where ibn.rowNum = @index
      ) ;
    
    set @drop_sql_tax = concat('drop table if exists ',@tb_name); 
      
    PREPARE distSQL FROM @drop_sql_tax ;
    EXECUTE distSQL;
    DEALLOCATE PREPARE distSQL ;

END WHILE;

drop table drop_tb;

end ;

call rush();

drop PROCEDURE if exists rush;

## THE END

## 變數設定
set @index = 0;
set @count = (select count(0) from drop_tb) ;

## 遍歷刪除字首為 aopi_copy 的表
WHILE @index <  @count DO
        
    set @index = @index + 1 ;
    set @tb_name = (
        select table_name from drop_tb as ibn
        where ibn.rowNum = @index
      ) ;
    
    set @drop_sql_tax = concat('drop table if exists ',@tb_name); 
      
    PREPARE distSQL FROM @drop_sql_tax ;
    EXECUTE distSQL;
    DEALLOCATE PREPARE distSQL ;

END WHILE;

drop table drop_tb;

end ;

call rush();

drop PROCEDURE if exists rush;

## THE END