-- 创建存储过程之前需判断该存储过程是否已存在,若存在则删除 DROP PROCEDURE IF EXISTS getTableInfo; -- 创建存储过程 CREATE PROCEDURE getTableInfo() BEGIN -- 定义变量 DECLARE s int DEFAULT 0; DECLARE dbname varchar(255); DECLARE tabname varchar(256); -- 定义游标,并将sql结果集赋值到游标中 DECLARE tabs CURSOR FOR select table_schema,table_name from dq_tables; -- 声明当游标遍历完后将标志变量置成某个值 DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=1; -- 创建结果存放表 create table if not exists `dq_tables` ( `table_schema` VARCHAR ( 255 ) DEFAULT NULL COMMENT '数据库名称', `dept_name` VARCHAR ( 256 ) DEFAULT NULL COMMENT '委办局名称', `table_name` VARCHAR ( 257 ) DEFAULT NULL COMMENT '表英文名称', `table_comment` VARCHAR ( 258 ) DEFAULT NULL COMMENT '表中文名称', `table_rows` VARCHAR ( 259 ) DEFAULT NULL COMMENT '数据行数', `data_size` VARCHAR ( 260 ) DEFAULT NULL COMMENT '表大小', `create_time` VARCHAR ( 261 ) DEFAULT NULL COMMENT '创建时间', `update_time` VARCHAR ( 262 ) DEFAULT NULL COMMENT '更新时间', `is_cp` VARCHAR ( 262 ) DEFAULT NULL COMMENT '是否统计列完整性', `update_type` VARCHAR ( 255 ) DEFAULT NULL COMMENT '更新类型', `update_freq` VARCHAR ( 255 ) DEFAULT NULL COMMENT '更新频率', `db_type` VARCHAR ( 255 ) DEFAULT NULL COMMENT '入库方式' ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '数据质量-数据表相关信息'; -- 插入库表原始数据 insert into dq_tables select t.table_schema as 数据库名称, null , t.table_name as 表英文名称, t.table_comment as 表中文名称, null as 数据行数, ( t.data_length / 1024 ) as 表大小, t.create_time as 创建时间, t.update_time as 更新时间 , 1, null, null, null from information_schema.`tables` t left join dq_tables d on t.table_name = d.table_name and t.table_schema = d.table_schema where d.table_name is null and t.table_schema not in ('information_schema','mysql','performance_schema','test','sakila','sys') and t.table_name <> 'dq_tables'; -- 打开游标 open tabs; -- 当s不等于1,也就是未遍历完时,会一直循环 while s<>1 do -- 执行业务逻辑 fetch tabs into dbname,tabname; -- 更新数据行数 set @esql =CONCAT("update dq_tables set table_rows = (select count(1) from ",dbname,'.',tabname,") where table_schema='",dbname,"' and table_name='",tabname,"' "); PREPARE stmt FROM @esql; EXECUTE stmt ; end while; -- 当s等于1时表明遍历以完成,退出循环 -- 关闭游标 close tabs; DEALLOCATE PREPARE stmt; -- 释放连接 END;