1. CREATE DEFINER=`szsnwbmqzkdb`@`%` PROCEDURE `table_count_procedure`()
    2. BEGIN
    3. -- 运行前清空对应表结构
    4. TRUNCATE table_count;
    5. -- 将查询出来的库名、表名、拼接的sql语句写入表\
    6. -- 此处sql语句是将查询的count(1)结构赋值给@count变量!
    7. INSERT INTO test.table_count ( table_schema, table_name, table_sql ) SELECT
    8. TABLE_schema,
    9. table_name,
    10. CONCAT( "select count(1) from ", TABLE_schema, ".", table_name, " into @count",";" ) 'table_sql'
    11. FROM
    12. information_schema.TABLES
    13. WHERE
    14. table_name LIKE 'biz%';
    15. -- 定义开始、结束值、开始从id为、开始、结束为表的行数
    16. set @start_xh = 1;
    17. set @end_xh= (select count(1) from test.table_count);
    18. WHILE @start_xh < @end_xh+1 DO
    19. -- 查询需要运行的sql、即拼接的语句
    20. set @real_sql = (select table_sql from test.table_count where id = @start_xh);
    21. -- sql预处理
    22. PREPARE ex_sql from @real_sql;
    23. -- 使用execute执行sql
    24. EXECUTE ex_sql;
    25. -- 将变量的值赋值给表中的res_count字段
    26. update test.table_count set count_res=@count where id = @start_xh;
    27. -- 序号需要自增
    28. set @start_xh = @start_xh + 1;
    29. END WHILE;
    30. END