我想创建一段代码来计算我的架构中缺失和空白的数量。
我的想法是使用concat创建脚本来计算一列上的数字,然后循环遍历行数。
创建一个包含要执行的指令的表是可以的,但我在循环中挣扎。在脚本中包含P1时,没有任何反应。
有什么想法吗?
DROP TABLE IF EXISTS workspace.instructions;
CREATE TABLE workspace.instructions (
instructions varchar(3000)
) ;
insert into workspace.instructions
select
CONCAT('insert into workspace.missing_rows
select
sum(case when ', column_name, ' is null then 1 else 0 end) as nb_missing,
sum(case when ', column_name,' = "" then 1 else 0 end) as nb_blank,
"', table_name, '" as table_name0,
"', column_name, '" as column_name0
from my_schema.', table_name, '; ') as instruction
from information_schema.COLUMNS where TABLE_SCHEMA = 'my_table' and DATA_TYPE = 'varchar';
-- 57 rows
DROP TABLE IF EXISTS workspace.missing_rows;
CREATE TABLE workspace.missing_rows (
table_name0 varchar(64),
column_name0 varchar(64),
nb_missing int,
nb_blank int
) ;
DROP TABLE IF EXISTS workspace.test_loop;
CREATE TABLE workspace.test_loop (
test varchar(1500)
) ;
DROP PROCEDURE IF EXISTS doiterate;
DELIMITER $$
CREATE PROCEDURE doiterate(p1 INT)
BEGIN
label1: LOOP
SET p1 = p1 + 1;
SELECT @s = (select instructions from workspace.instructions limit p1, 1);
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
IF p1 < 57 THEN ITERATE label1; END IF;
LEAVE label1;
END LOOP label1;
END;
call doiterate(0);
select * from workspace.missing_rows;