日期:2014-05-16 浏览次数:20783 次
drop procedure if exists pr_statis_rows; create procedure pr_statis_rows( db1 varchar(50), db2 varchar(50) ) begin DECLARE done INT DEFAULT 0; declare tempTableName varchar(50); declare stmt varchar(2000); DECLARE cur CURSOR FOR select table_name from INFORMATION_SCHEMA.TABLES where table_type='BASE TABLE' and table_schema=db1; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; create TEMPORARY table IF NOT EXISTS ecard_row (table_name varchar(50), rows_ int, table_name2 varchar(50), rows_2 int); -- 打开 /* select db1; */ # 打印db1 OPEN cur; REPEAT FETCH cur INTO tempTableName; if tempTableName<>'ecard_row' then IF NOT done THEN set @c1=0; set @c2=0; set @sql = concat('select count(*) from ', db1 ,'.', tempTableName, ' into @c1'); set @sql2 = concat('select count(*) from ', db2 ,'.', tempTableName, ' into @c2'); prepare stmt from @sql; execute stmt; prepare stmt from @sql2; execute stmt; Insert ecard_row values (tempTableName, @c1, tempTableName, @c2); END IF; end if; UNTIL done END REPEAT; CLOSE cur; # 不能有return end; delete from ecard.ecard_row; # 不能省略参数 call ecard.pr_statis_rows('aa','bb'); select * from ecard.ecard_row show procedure status