'Missing values percentage in MYSQL

I want to find the missing values percentage for each column in table name passed in missing_data_perc function parameter.

Delimiter $$
CREATE PROCEDURE missing_data_perc(IN tableName VARCHAR(30))
BEGIN
DECLARE i int default 0;
DECLARE n int default 0;
DECLARE columnName VARCHAR(30);
DECLARE perc int;
SET @tname = tableName;
SET @perc = 0;

DROP TEMPORARY TABLE IF EXISTS missing_data;
Create TEMPORARY TABLE missing_data (
data_name VARCHAR(50),
missing_data_percentage int
);

SELECT count(*) from information_schema.columns where table_name = tableName into n;
SET i=0;
while i < n Do
--  Select column_name from information_schema.columns where table_name = tableName limit i,1 into columnName;
--  Set @Q3 = CONCAT('SELECT 1-count(*)/count(columnName) from ? into ?');
--  PREPARE Stmt3 from @Q3;
--  EXECUTE Stmt3 using @tname, @perc;
--  SET perc = @perc;
    SELECT 1-count(*)/count(columnName) from tableName into perc;
    INSERT into missing_data VALUES (columnName, perc);
    SET i = i+1;
End While;

SELECT * from missing_data;
END; $$
Delimiter ;

CALL missing_data_perc("deliveries");

Below line is giving me error:

SELECT 1-count(*)/count(columnName) from tableName into perc;

But if i change it to below statement then i works,

SELECT 1-count(*)/count(columnName) from deliveries into perc;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source