– 创建表mysqls_tables:
create table mysqls_tables(tab_owner varchar(100),tab_name varchar(100),tab_count int);
– 执行存储过程:
delimiter //
create or replace procedure p_get_table_row_count()
begin
truncate table mysqls_tables;
begin
declare done int default 0;
declare tbl_name varchar(255);
declare cur cursor for
select table_name
from information_schema.tables
where table_schema = '数据库名称';
declare continue handler for not found set done = 1;
open cur;
read_loop: loop
fetch cur into tbl_name;
if done then
leave read_loop;
end if;
set @sql = concat('select count(*) into @count from ', tbl_name);
prepare stmt from @sql;-- 定义预处理语句
execute stmt;-- 执行预处理语句
deallocate prepare stmt;-- 释放资源
if tbl_name = 'mysqls_tables' then
select count(*) into @count
from information_schema.tables
where table_schema = '数据库名称';
set @count = @count;
end if ;
insert into mysqls_tables (
tab_owner,
tab_name,
tab_count)
values (
'数据库名称',
tbl_name,
@count);
commit;
end loop;
close cur;
end;
end //
delimiter ;
– 查询mysqls_tables
select * from mysqls_tables;
运行版本10.6.14