存儲過程 遍歷表 並保存數據 彙總數據方便查詢

DROP PROCEDURE IF EXISTS FountTable;
delimiter $$
create procedure FountTable()
begin
    declare TableName varchar(64);   
        declare i int;   
     
    DECLARE cur_FountTable CURSOR FOR  
select CONCAT(keyEn,"details") from coins ;
    DECLARE EXIT HANDLER FOR not found CLOSE cur_FountTable;
    #打開遊標
    OPEN cur_FountTable;
    REPEAT
     FETCH cur_FountTable INTO TableName;
         SELECT count(*) into i FROM information_schema.TABLES WHERE table_name =TableName; 
         if i>0 then 
     SET @SQLSTR1 = CONCAT("insert into alldetails(coinsName,amount,sendTime,userName,opUnique) select '",TableName,"', amount,sendTime,userName,opUnique from ",TableName," where status=2 and sendTime > '2019-03-04 20:00:00' and sendTime <  '2019-03-04 23:59:59' ");
     PREPARE STMT1 FROM @SQLSTR1;
     EXECUTE STMT1;
         DEALLOCATE PREPARE STMT1;    
         
         end if;
       
     UNTIL 0 END REPEAT;
  #關閉遊標
  CLOSE cur_FountTable;
  
END $$
DELIMITER ;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章