Mysql stored procedure "fuzzy query drop table"

need:

清空 show tables like "%2016%"

accomplish:

DELIMITER //  

drop procedure if exists p_clean_table;
create PROCEDURE p_clean_table(in tab varchar(128))
begin
    DECLARE name VARCHAR(64);
     -- traverse the end of data flag
    DECLARE done INT DEFAULT FALSE;
    DECLARE tables_cur CURSOR FOR select table_name from information_schema.tables where table_schema='eqiurong' and table_name like '%2016%';
    -- bind the end marker to the cursor
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    -- open cursor
   OPEN tables_cur;
     -- start loop
   read_loop: LOOP
    -- Extract the data in the cursor, there is only one here, and the same is true for multiple;
    FETCH tables_cur INTO name;
    -- at the end of the statement
    IF done THEN
      LEAVE read_loop;
    END IF;
    -- here do what you want to do in the event loop
    set @bsql :=concat("drop table if exists ",name);
	PREPARE STMT FROM  @bsql;
	EXECUTE STMT  ;
   END LOOP;
  -- close cursor
  CLOSE tables_cur;
end;
//
DELIMITER ;

 

ps:

Stored procedure syntax:

create PROCEDURE p_xx(in p1 varchar(128),out p2 varchar(128))
begin
............
end;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038517&siteId=291194637