批量获取一个数据库中所有表记录数的存储过程
CREATE PROCEDURE "fnGetTablesCount"()
BEGIN
declare tableName varchar(50);
DECLARE DONE INT DEFAULT(0);
declare curTab cursor for select table_name from information_schema.tables where table_schema='courseware';
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
drop table if exists tabcount;
create temporary table tabcount(tabname varchar(50), rowcnt int);
open curTab;
repeat
fetch curTab into tableName;
if not DONE then
#set tableName = 'student';
set @sSql = concat('select count(*) into @cnt from ', tableName);
#select sSql;
prepare stmt from @sSql; # 预处理声明中必须用 Session 变量
execute stmt;# into cnt;
#select tableName || ' 行数:' || @cnt;
insert into tabcount values(tableName, @cnt);
#select found_rows() '行数';
DEALLOCATE prepare stmt;
end if;
UNTIL DONE END REPEAT;
close curTab;
select * from tabcount;
drop table if exists tabcount;
END
评论
热门帖子
- 12025-12-01浏览数:182759
- 22023-05-09浏览数:25044
- 42023-09-25浏览数:18519
- 52020-05-11浏览数:17526